a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 102|回复: 1

[基础知识] JAVA基础:Java连接各种数据库方法

[复制链接]
发表于 2012-8-4 12:37:27 | 显示全部楼层 |阅读模式
1、Oracle8/8i/9i数据库(thin模式)   Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();; u7 h) l# R1 c1 ], N% ~
  String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
8 G4 [( Q* [* I  W1 @* X  String user="test";
1 |6 z6 e+ Q, {3 n8 [  String password="test";7 g: h+ f# \9 t6 K, G& k
  Connection conn= DriverManager.getConnection(url,user,password);
7 H" N! P; W+ v( e2、DB2数据库 % b# r" z( N5 `* C
  Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();. Z7 M! ?3 a( W# |$ `/ s2 v) K) A
  String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名4 c2 Q1 P7 Z9 k, @0 u* C
  String user="admin";
- T  n  r9 c- G; {" e  String password="";" h+ v% \) k1 K" L1 S9 \
  Connection conn= DriverManager.getConnection(url,user,password);; H! ~% R! y& H! N  w5 m
3、SQL Server7.0/2000数据库 : J6 W1 W, T6 G. w+ ^4 \; O1 ~
  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
0 }6 z6 S$ ?* m8 p) l  String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";: t+ [/ J. `0 O* t' L! e
  //mydb为数据库* |! d2 H) }3 e" L+ I) X0 ^
  String user="sa";& L. R+ V2 o. \6 r4 g
  String password="";1 {+ O# y9 s6 `/ _9 S
  Connection conn= DriverManager.getConnection(url,user,password);
: i% L# F$ p6 h! P4、Sybase数据库 ; ^. }4 O0 Y" A4 j: x0 o0 s
  Class.forName("com.sybase.jdbc.SybDriver").newInstance();
1 R5 s- T0 u% U4 p! T7 g4 ]) z  String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名8 m: {4 k* b+ |
  Properties sysProps = System.getProperties();
, H. [* X: o9 ~$ Z( [8 w* c  SysProps.put("user","userid");8 R$ R+ l* W/ E$ r5 h% R
  SysProps.put("password","user_password");
/ M, G8 L& O: p; v" y! ]8 |% @3 S8 a- A5 f* l+ M' J+ G1 [( E  [
  Connection conn= DriverManager.getConnection(url, SysProps);
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:37:28 | 显示全部楼层

JAVA基础:Java连接各种数据库方法

</p>5、Informix数据库 ) d8 Y( C! U( z. w& V* ~
  Class.forName("com.informix.jdbc.IfxDriver").newInstance();
- P- w) z. ]1 A. l0 a/ Z  String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
9 Y5 E8 T5 d: p6 i  user=testuser;password=testpassword"; //myDB为数据库名
7 {5 T2 }# f% b, X! P  Connection conn= DriverManager.getConnection(url);4 S, x8 `7 c9 _% A! w8 J
6、MySQL数据库
( `8 I3 b# p) x: v; v, ]  Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  B# h' U- B! Y  q; J7 k4 G8 _! E  //或者Class.forName("com.mysql.jdbc.Driver");; h! U7 M$ V- s. s
  String url ="jdbc:mysql://localhost/myDB?
- c. @; \+ _. Q/ w0 N  user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
) u+ p% c* \. e! D" O% `  //myDB为数据库名( P7 N9 a0 X8 v& M6 b) s; Z6 _
  Connection conn= DriverManager.getConnection(url);+ P* p" r5 w: ^1 c* w
7、PostgreSQL数据库   @; H* V2 |. y$ [; ~( h  ?- E! E
  Class.forName("org.postgresql.Driver").newInstance();
7 w7 s+ B# v, D7 H- U  String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名, s. `) Y. J1 C2 b, I) Q, R7 U
  String user="myuser";$ {0 u( M0 T# q% W( L* j
  String password="mypassword";
5 N: B0 x: \; W! J7 R  Connection conn= DriverManager.getConnection(url,user,password);' r1 I9 ]' K3 V) m
8、access数据库直连用ODBC的
/ h+ k7 A7 y# i2 a9 W& z4 `! v  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;2 V5 q4 }% X6 P
  String url="jdbc:odbc:Driver={MicroSoft Access Driver# m4 I6 u: }, D& L, z
  (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");  _& X9 f  K3 ^
  Connection conn = DriverManager.getConnection(url,"","");. j  L" @9 M# x7 ^( C* }
  Statement stmtNew=conn.createStatement() ;
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-4-29 16:14 , Processed in 0.215630 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表