首页 > 代码库 > JDBC连接数据库核心代码
JDBC连接数据库核心代码
1.Oracle数据库
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:orcle";
String user = "test";
String password = "test";
Connection conn = DriverManager.getConnection(url, user, password);
2.MySQL数据库
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost/testDB?user=testuser&password=testpassword&useUnicode=true&characterEncoding=GB2312";
Connection conn = DriverManager.getConnection(url);
3.SQL Server数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDB";
String user = "sa";
String password = "test";
Connection conn = DriverManager.getConnection(url, user, password);
4. Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url = "jdbc:sybase:Tds:localhost:5007/testDB";
Properties sysProps = System.getProperties();
sysProps.put("user", "userid");
sysProps.put("password", "user_password");
Connection conn = DriverManager.getConnection(url, sysProps);
5.Informix数据库
Class.forName("com.infoxmix.jdbc.IfxDriver").newInstance();
String url = "jdbc:infoxmix-sqli://localhost:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";
Connection conn = DriverManager.getConnection(url);
6.DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
String url = "jdbc:db2://localhost:5000/testDB";
String user = "admin";
String password = "test";
Connection conn = DriverManager.getConnection(url, user, password);
7.PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url = "jdbc:postgresql://localhost/testDB";
String user = "myuser";
String password = "test";
Connection conn = DriverManager.getConnection(url, user, password);
JDBC连接数据库核心代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。