当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>J2EE

浅谈JDBC怎么连接数据库

    各个开发商的接口并不完全相同,所以开发环境的变化会带来一定的配置变化。本文主要集合了不同数据库的连接方式。

    一、连接各种数据库方式速查表

    下面罗列了各种数据库使用JDBC连接的方式,可以作为一个手册使用。

    1、Oracle8/8i/9i数据库(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Stringurl="jdbcracle:thinlocalhost:1521rcl";//orcl为数据库的SID
Stringuser="test";
Stringpassword="test";
Connectionconn=DriverManager.getConnection(url,user,password);

    2、DB2数据库

Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
Stringurl="jdbc:db2://localhost:5000/sample";//sample为你的数据库名
Stringuser="admin";
Stringpassword="";
Connectionconn=DriverManager.getConnection(url,user,password);

    3、SqlServer7.0/2000数据库

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Stringurl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
Stringuser="sa";
Stringpassword="";
Connectionconn=DriverManager.getConnection(url,user,password);

    4、Sybase数据库

Class.forName("com.sybase.jdbc.SybDriver").newInstance();
Stringurl="jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名
PropertiessysProps=System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connectionconn=DriverManager.getConnection(url,SysProps);

    5、Informix数据库

Class.forName("com.informix.jdbc.IfxDriver").newInstance();
Stringurl="jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";//myDB为数据库名
Connectionconn=DriverManager.getConnection(url);

    6、MySQL数据库

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Stringurl="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB为数据库名
Connectionconn=DriverManager.getConnection(url);

    7、PostgreSQL数据库

Class.forName("org.postgresql.Driver").newInstance();
Stringurl="jdbc:postgresql://localhost/myDB"//myDB为数据库名
Stringuser="myuser";
Stringpassword="mypassword";
Connectionconn=DriverManager.getConnection(url,user,password);

    8、access数据库直连用ODBC的

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Stringurl="jdbcdbcriver={MicroSoftAccessDriver(*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connectionconn=DriverManager.getConnection(url,"","");
StatementstmtNew=conn.createStatement();

共3页 首页 上一页 1 2 3 下一页 尾页 跳转到
相关内容
赞助商链接