Java使用JDBC串連任意類型資料庫(mysql oracle。。),oraclejdbc

來源:互聯網
上載者:User

Java使用JDBC串連任意類型資料庫(mysql oracle。。),oraclejdbc

package cn.liz.test;import java.io.InputStream;import java.sql.Connection;import java.sql.Driver;import java.sql.SQLException;import java.util.Properties;import org.junit.Test;public class JBDCtest {/** * 編寫一個通用的方法, 在不修改來源程式的情況下, 可以擷取任何資料庫的串連 * 解決方案: 把資料庫驅動 Driver 實作類別的全類名、url、user、password 放入一個 * 設定檔中, 通過修改設定檔的方式實現和具體的資料庫解耦.  * @throws Exception  */public  Connection getConnection() throws Exception{String driverClass = null;String jdbcUrl = null;String user = null;String password = null;//讀取類路徑下的 jdbc.properties 檔案InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");Properties properties = new Properties();properties.load(in);driverClass = properties.getProperty("driver");jdbcUrl = properties.getProperty("jdbcUrl");user = properties.getProperty("user");password = properties.getProperty("password");//通過反射常見 Driver 對象. Driver driver = (Driver) Class.forName(driverClass).newInstance();Properties info = new Properties();info.put("user", user);info.put("password", password);//通過 Driver 的 connect 方法擷取資料庫連接. Connection connection = driver.connect(jdbcUrl, info);return connection;}@Testpublic void testGetConnection() throws Exception{System.out.println(getConnection());}}<span style="white-space:pre"></span>
<span style="font-family: Arial, Helvetica, sans-serif;">jdbc.properties</span>設定檔:
#driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl#user=scott#password=javadriver=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/testuser=rootpassword=

java JDBC串連不同的資料庫寫法sql,oracle,mysql下JDBC串連各種資料庫寫法獻給Java初學者提點建議記得匯入對應驅動jar包哦

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資料庫

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

Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost/myDB? ser=root&password=system"; conn = DriverManager.getConnection(url);

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); 補充:Sql Server2005資料庫

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb資料庫 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);
和sql2000 串連url有點區別JDBC串連資料庫方法基本上面些了

網上相當多了查有哦DriverClass比較好寫通過包點出來
主要url過url也改下當前資料庫對應設定了
JAVA 使用JDBC串連MYSQL資料庫時,串連不同資料庫的方法建立另外connection對象兩對象操作同資料庫
url1="jdbc:Mysql://127.0.0.1:3306/comm";

url2="jdbc:Mysql://127.0.0.1:3306/swj2";
...
...
dbConnection1=DriverManager.getConnection(url1, user, password);

dbConnection2=DriverManager.getConnection(url2, user, password);

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.