此文中的代码主要列出连接数据库的关键代码,其他访问数据库代码省略
1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);
6、MySQL数据库
public static String driverClass = "com.mysql.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/testInsert";
public static String username = "root";
public static String password = "wq3892961";
7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
补充1:
连接Oracle数据库
获得Oracle的驱动程序包classes12.jar
把Oracle的驱动程序到\WEB-INF\lib下
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:数据库","用户名","密码");
连接MYSQL数据库
获得MySQL的驱动程序包mysql-connector-java-3.0.15-ga-bin(mysqldriver.jar)
拷贝到\WEB-INF\lib下
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/testDB","用户名","密码" );
连接Sql 2000数据库
获得SQL直接的驱动程序包msbase.jar和mssqlserver.jar和msutil.jar
把直?驱动程序包放到\WEB-INF\lib下
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection conn= DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库"","用户名","密码");
补充2:
MYSQL
datasource.url=jdbc:mysql://localhost:3306/bbscs6?useUnicode=true&characterEncoding=UTF-8
datasource.username=root
datasource.password=root
Oracle
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@192.168.0.1:1521:SID
datasource.username=bbscs
datasource.password=bbscs
datasource.defaultAutoCommit=true
hibernate.dialect=net.sf.hibernate.dialect.OracleDialect
如果是Oracle9,写
hibernate.dialect=net.sf.hibernate.dialect.Oracle9Dialect
SQL Server
datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
datasource.url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbscs
datasource.username=bbscs
datasource.password=bbscs
datasource.defaultAutoCommit=true
hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);
6、MySQL数据库
public static String driverClass = "com.mysql.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/testInsert";
public static String username = "root";
public static String password = "wq3892961";
7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
补充1:
连接Oracle数据库
获得Oracle的驱动程序包classes12.jar
把Oracle的驱动程序到\WEB-INF\lib下
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:数据库","用户名","密码");
连接MYSQL数据库
获得MySQL的驱动程序包mysql-connector-java-3.0.15-ga-bin(mysqldriver.jar)
拷贝到\WEB-INF\lib下
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/testDB","用户名","密码" );
连接Sql 2000数据库
获得SQL直接的驱动程序包msbase.jar和mssqlserver.jar和msutil.jar
把直?驱动程序包放到\WEB-INF\lib下
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection conn= DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库"","用户名","密码");
补充2:
MYSQL
datasource.url=jdbc:mysql://localhost:3306/bbscs6?useUnicode=true&characterEncoding=UTF-8
datasource.username=root
datasource.password=root
Oracle
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@192.168.0.1:1521:SID
datasource.username=bbscs
datasource.password=bbscs
datasource.defaultAutoCommit=true
hibernate.dialect=net.sf.hibernate.dialect.OracleDialect
如果是Oracle9,写
hibernate.dialect=net.sf.hibernate.dialect.Oracle9Dialect
SQL Server
datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
datasource.url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbscs
datasource.username=bbscs
datasource.password=bbscs
datasource.defaultAutoCommit=true
hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect