Java技巧:Java连接数据库技巧全攻略

数据库
编者在本文将主要为大家介绍Java与Oracle、DB2、Sql Server、Sybase、MySQL、PostgreSQL等数据库连接的方法。

编者在本文将主要为大家介绍Java与Oracle、DB2、Sql Server、Sybase、MySQL、PostgreSQL等数据库连接的方法。

1、Oracle数据库

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);  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

  #p# 

 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);  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

  #p#

3、Sql Server数据库

  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);  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

 #p#

 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);  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 #p#

 5、MySQL数据库

  Class.forName("org.gjt.mm.mysql.Driver").newInstance();  
 
  String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" 
 
  //myDB为数据库名  
 
  Connection conn= DriverManager.getConnection(url);  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

 #p#

 6、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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
责任编辑:艾婧 来源: it168网站原创
相关推荐

2015-03-04 13:53:33

MySQL数据库优化SQL优化

2023-10-13 19:42:00

2011-07-19 20:36:56

2009-12-15 18:03:46

Ruby连接数据库

2010-08-10 09:53:47

DB2数据库补丁

2010-01-07 17:24:12

VB.NET连接数据库

2024-11-26 10:01:25

2025-01-21 08:10:00

2010-05-26 11:22:08

2024-02-22 09:25:09

Python字典推导式开发

2013-04-15 10:48:16

Xcode ARC详解iOS ARC使用

2024-05-07 09:01:21

Queue 模块Python线程安全队列

2013-06-08 11:13:00

Android开发XML解析

2023-10-12 07:29:24

MySQL分页数据量

2010-04-23 14:04:23

Oracle日期操作

2009-07-17 15:34:37

Java Swing连接数据库

2011-03-10 11:17:03

数据库设计技巧

2009-12-14 14:32:38

动态路由配置

2009-10-19 15:20:01

家庭综合布线

2014-03-19 17:22:33

点赞
收藏

51CTO技术栈公众号