最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

經(jīng)常使用的JDBC連接數(shù)據(jù)庫(kù)方式

 更新時(shí)間:2013年04月07日 17:07:02   作者:  
在我們開(kāi)發(fā)中,幾乎脫離不了連接數(shù)據(jù)庫(kù)。并且無(wú)論是使用框架還是硬編碼連接數(shù)據(jù)庫(kù),都避免不了寫(xiě)驅(qū)動(dòng)類以及連接url。為了方便我們的開(kāi)發(fā),我們收藏常用的jdbc連接數(shù)據(jù)庫(kù)方式。

一、JDBC連接DB2

復(fù)制代碼 代碼如下:

Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://dburl:port/DBname"
cn = DriverManager.getConnection( url, sUsr, sPwd );

二、JDBC連接Microsoft SQLServer(microsoft)

復(fù)制代碼 代碼如下:

Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master", sUsr, sPwd );

三、JDBC連接Sybase(jconn2.jar)

復(fù)制代碼 代碼如下:

Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
cn = DriverManager.getConnection( "jdbc:sybase:Tds:DBServerIP:2638", sUsr, sPwd );

四、JDBC連接MySQL(mm.mysql-3.0.2-bin.jar)

復(fù)制代碼 代碼如下:

Class.forName( "org.gjt.mm.mysql.Driver" );
cn = DriverManager.getConnection( "jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd );

五、JDBC連接PostgreSQL(pgjdbc2.jar)

復(fù)制代碼 代碼如下:

Class.forName( "org.postgresql.Driver" );
cn = DriverManager.getConnection( "jdbc:postgresql://DBServerIP/myDatabaseName", sUsr, sPwd );

六、JDBC連接Oracle(classes12.jar)

復(fù)制代碼 代碼如下:

Class.forName( "oracle.jdbc.driver.OracleDriver" );
cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );

七、JDBC連接ODBC

復(fù)制代碼 代碼如下:

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );

有些數(shù)據(jù)庫(kù)的jdbc連接方法并不是固定的,要看你用的驅(qū)動(dòng)包。
例如mssql的jtdsjar包:
數(shù)據(jù)庫(kù)URL:jdbc:jtds:sqlserver://localhost:1433;DatabaseName=XXX
驅(qū)動(dòng)類:net.sourceforge.jtds.jdbc.Driver


1、Oracle8/8i/9i數(shù)據(jù)庫(kù)(thin模式)

復(fù)制代碼 代碼如下:

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為數(shù)據(jù)庫(kù)的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2、DB2數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample"; //sample為你的數(shù)據(jù)庫(kù)名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

3、Sql Server7.0/2000數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為數(shù)據(jù)庫(kù)
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

4、Sybase數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB為你的數(shù)據(jù)庫(kù)名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);

5、Informix數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

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為數(shù)據(jù)庫(kù)名
Connection conn= DriverManager.getConnection(url);

6、MySQL數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

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為數(shù)據(jù)庫(kù)名
Connection conn= DriverManager.getConnection(url);

7、PostgreSQL數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB" //myDB為數(shù)據(jù)庫(kù)名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);

8、access數(shù)據(jù)庫(kù)直連用ODBC的

復(fù)制代碼 代碼如下:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");

 

相關(guān)文章

最新評(píng)論

海兴县| 遂宁市| 江源县| 镶黄旗| 雅江县| 赣榆县| 鲜城| 天镇县| 贵州省| 浮梁县| 象州县| 蛟河市| 永嘉县| 游戏| 平阴县| 建德市| 九龙县| 顺昌县| 柳江县| 溧阳市| 高尔夫| 龙陵县| 兴城市| 贵定县| 枣强县| 城固县| 德安县| 修武县| 宝清县| 墨竹工卡县| 安阳市| 江口县| 南华县| 都江堰市| 宝坻区| 咸宁市| 江门市| 遂川县| 栾城县| 黄龙县| 凌海市|