1.三種注冊(cè)驅(qū)動(dòng)器的方法
(1)Class.forName("com.MySQL.jdbc.Driver");
(2)System.set (3)極不推薦 DriverManager.registerDrivers(new com.mysql.jdbc.Driver()); 2.三種獲取連接的方式 (1)Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); (2)Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&passWord=123456"); (3)Connection conn=DriverManager.getConnection(strUrl,props); database.properties配置文件 jdbc.drivers=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.user=root jdbc.password=123456 ... Properties props=new Properties(); FileInputStream in=new FileInputStream("database.properties"); //獲取database.properties配置文件 props.load(in); in.close(); String strDriver=props.getProperty("jdbc.drivers"); String strUrl=props.getProperty("jdbc.url"); System.setProperty("jdbc.drivers",strDriver); Connection conn=DriverManager.getConnection(strUrl,props); 3.追記 在JDBC4.0中已經(jīng)不需要再顯示的注冊(cè)驅(qū)動(dòng)了 以下是JDK6.0文檔中的建議: JDBC 4.0 驅(qū)動(dòng)程序包中必須包括 應(yīng)用程序不再需要使用META-INF/services/java.sql.Driver文件。此文件包含java.sql.Driver的 JDBC 驅(qū)動(dòng)程序?qū)崿F(xiàn)的名稱。例如,要加載my.sql.Driver類,META-INF/services/java.sql.Driver文件需要包含下面的條目:
my.sql.DriverClass.forName()顯式地加載 JDBC 驅(qū)動(dòng)程序。當(dāng)前使用Class.forName()加載 JDBC 驅(qū)動(dòng)程序的現(xiàn)有程序?qū)⒃诓蛔餍薷牡那闆r下繼續(xù)工作。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注