您的位置:首页 > 数据库

使用JDBC连接各种数据库(总结)

2009-07-01 23:23 573 查看
1、 连接Oracle 8/8i/9i数据库

Class.forName("oracle.jdbc.driver.OracleDriver");

String url=”jdbc: oracle:thin:@192.168.0.1:1521:orcl”;

String user=”test”;

String password=”test”;

Connection con = DriverManager.getConnection(url,user,password);

oracle.jdbc.driver.OracleDriver:驱动程序类的名称

jdbc: oracle:thin: 使用thin 模式连接

192.168.0.1: 数据库的ip 地址

1521: 数据库服务的端口号。这是Oracle的默认值。

Orcl: 数据库的SID

User: 访问数据库的用户名。

Password: 访问数据库的密码。

2、 连接DB2数据库

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

String url=”jdbc:db2://127.0.0.1:5000:sample”;

String user=”admin”;

String password=””;

Connection con = DriverManager.getConnection(url,user,password);

com.ibm.db2.jdbc.app.DB2Driver:驱动程序类的名称

127.0.0.1: 数据库的ip 地址

5000: 数据库服务的端口号。

Sample: 数据库的名称。

User: 访问数据库的用户名。

Password: 访问数据库的密码。

3、 连接SQL Server 7.0/2000数据库

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

String url=”jdbc: microsoft:sqlserver://localhost:1433;DatabaseName=mydb”;

String user=”sa”;

String password=””;

Connection con = DriverManager.getConnection(url,user,password);

com.microsoft.jdbc.sqlserver.SQLServerDriver:驱动程序类的名称

localhost: 数据库的地址

1433: 数据库服务的端口号。

Mydb: 数据库的名称

User: 访问数据库的用户名。

Password: 访问数据库的密码。

4、 连接Sybase数据库

Class.forName("com.sybase.jdbc.SybDriver");

String url=”jdbc:sybase:Tds:localhost:5007/myDB”;

Properties sysProps=System.getProperties();

SysPros.put(“user”,”userid”);

SysPros.put(“password”,”user_password”);

Connection con = DriverManager.getConnection(url);

com.sybase.jdbc.SybDriver:驱动程序类的名称

localhost: 数据库的地址

5007: 数据库服务的端口号。

Mydb: 数据库的名称

Userid: 访问数据库的用户名。

User_password: 访问数据库的密码。

5、 连接Informix数据库

Class.forName("com.informix.jdbc.IfxDriver");

String url=”jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=

myserver;user=testuser;password=testpassword”;

Connection con = DriverManager.getConnection(url);

com.informix.jdbc.IfxDriver:驱动程序类的名称

123.45.67.89: 数据库的地址

1533: 数据库服务的端口号。

Mydb: 数据库的名称

Myserver:数据库服务器的名称。

Tstuser: 访问数据库的用户名。

Tsetpassword: 访问数据库的密码。

6、 连接MySQL数据库

Class.forName("org.git.mm.mysql.Driver");

Stringurl=”jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8895_1”

Connection con = DriverManager.getConnection(url);

org.git.mm.mysql.Driver:驱动程序类的名称

localhost: 数据库的地址

Mydb: 数据库的名称

Soft:访问数据库的用户名。

Soft1234: 访问数据库的密码。

8895_1: 使用的字符集。

7、 连接PostgreSQL数据库

Class.forName("org.postgresql.Driver");

String url=”jdbc:postgresql://localhost/myDB”;

String user=”myuser”;

String password=”mypassword”;

Connection con = DriverManager.getConnection(url,user,password);

org.postgresql.Driver:驱动程序类的名称

localhost: 数据库的地址

Mydb: 数据库的名称

Myuser:访问数据库的用户名。

Mypassword: 访问数据库的密码。

8、 连接Access数据库

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url=”jbdc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=”+application.getRealPath(“/Date/ReportDemo.mdb”)

Connection con = DriverManager.getConnection("jdbc:odbc:sun", "", "");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: