Java程式連線各種資料庫的方法

2021-08-25 15:06:27 字數 2521 閱讀 9002

[color=red] 1、oracle8/8i/9i資料庫(thin模式)[/color]

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);

[color=red]  2、db2資料庫[/color]

string url="jdbc:db2://localhost:5000/sample"; //sample為你的資料庫名

string user="admin";

string password="";

connection conn= drivermanager.getconnection(url,user,password);

[color=red]  3、sql server7.0/2000資料庫[/color]

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);

[color=red]  4、sybase資料庫[/color]

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);

[color=red]  5、informix資料庫[/color]

class.forname("com.informix.jdbc.ifxdriver").newinstance();

string url = "jdbc:informix-sqli:

user=testuser;password=testpassword"; //mydb為資料庫名

connection conn= drivermanager.getconnection(url);

[color=red]

6、mysql資料庫[/color]

class.forname("org.gjt.mm.mysql.driver").newinstance();

//或者class.forname("com.mysql.jdbc.driver");

string url ="jdbc:mysql://localhost/mydb?

user=soft&password=soft1234&useunicode=true&characterencoding=8859_1"

//mydb為資料庫名

connection conn= drivermanager.getconnection(url);

[color=red]  7、postgresql資料庫[/color]

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);

[color=red]  8、access資料庫直連用odbc的[/color]

class.forname("sun.jdbc.odbc.jdbcodbcdriver") ;

string url="jdbc:odbc:driver={microsoft access driver

connection conn = drivermanager.getconnection(url,"","");

statement stmtnew=conn.createstatement() ;

Java連線各種資料庫

此文中的 主要列出連線資料庫的關鍵 其他訪問資料庫 省略 1 oracle8 8i 9i資料庫 thin模式 class.forname oracle.jdbc.driver.oracledriver newinstance string url jdbc oracle thin localhost...

java連線各種資料庫

1 oracle8 8i 9i資料庫 thin模式 class.forname oracle.jdbc.driver.oracledriver newinstance string url jdbc oracle thin localhost 1521 orcl orcl為資料庫的sid strin...

java各種資料庫連線

mysql string driver com.mysql.jdbc.driver 驅動程式 string url jdbc mysql localhost 3306 db name 連線的url,db name為資料庫名 string username username 使用者名稱 string ...