闫平平
写代码也要酷酷的!

导航

 

JDBC连接各种数据库的方法:

 

JDBC编程步骤:

1、导入jar包

2、注册驱动

3、获取数据库连接对象

4、定义SQL语句

5、获得执行SQL语句对象statemnet

6、执行SQL语句

7、处理结果

8、释放资源

 

1)JDBC连接Oracle数据库

Class.forName("oracle.JDBC.driver.OracleDriver").newInstance();  //注册驱动

Connection con=DriverManager.getConnection("JDBC:oracle:thin:@localhost:1521:orcl",“你的oracle数据库用户名”,“用户名密码”);  //获取数据库连接对象

 

2)JDBC连接MySql数据库

class.forName("com.mysql.jdbc.Driver");  //注册驱动

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","你的mysql数据库用户名","用户名密码");  //获取数据库对象

 

3)JDBC连接DB2

Class.forName("com.ibm.db2.jcc.DB2Driver");  //注册驱动

Connection con=DriverManager.getConnection(“JDBC:db2://localhost:5000/testDb”,user,password);  //获取数据库对象

 

4)JDBC 连接SQL server

Class.forName("com.microsoft.JDBC.sqlserver.SQLServerDriver");   //注册驱动

Connection con=DriverManager.getConnection(“JDBC:microsoft:sqlserver://localhost:1433;DatabaseName=testDb”,user,password);  //获取数据库对象

 

5)连接PostgreSQL数据库

Class.forName("org.postgresql.Driver");  //注册驱动

Connection con=DriverManager.getConnection("JDBC:postgresql://localhost/testDb",user,password);  //获取数据库对象

 

6)连接Access数据库

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  //加载驱动

String url="JDBC:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/testDb/mdb");  //路径

Connection conn=DriverManager.getConnection(url,"","");  //获取数据库对象

 

7)连接informix数据库

Class.forName("com.informix.JDBC.ifxDriver");

String url="JDBC:informix-sqli:localhost:1533/testDb:INFORMIXSERVER=myserver"user=testUser;password=testpassword";

Connection con=DriverManager.getConnection(url);

 

8)连接Sybase数据库

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

String url="JDBC:sybase:Tds:localhost:5007/testDb";

Properties pro=System.getProperties();

pro.put("user","userId");

pro.put("password","user_password");

Connection con=DriverManager.getConnection(url,pro);

 

posted on 2019-03-30 15:43  写代码也要酷酷的  阅读(271)  评论(0编辑  收藏  举报