esclipse連接mysql數(shù)據(jù)庫
發(fā)表時(shí)間:2023-08-26 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]怎樣在eclipse開發(fā)環(huán)境中連接數(shù)據(jù)庫并測試連接是否成功1)eclipse開發(fā)環(huán)境里沒有集成mysql的驅(qū)動(dòng),需要從以下地址下載連接驅(qū)動(dòng)程序mysql-connector-java-XX-XX-XX.zip:http://dev.mysql.com/downloads/connector/j2)...
怎樣在eclipse開發(fā)環(huán)境中連接數(shù)據(jù)庫并測試連接是否成功1)eclipse開發(fā)環(huán)境里沒有集成mysql的驅(qū)動(dòng),需要從以下地址下載連接驅(qū)動(dòng)程序mysql-connector-java-XX-XX-XX.zip:
http://dev.mysql.com/downloads/connector/j
2)解壓,只取文件mysql-connector-java-XX.XX.XX-bin.jar,將其引用到你需要連接mysql數(shù)據(jù)庫的工程里,如:我在eclipsel里建了一個(gè)測試數(shù)據(jù)庫連接的工程ConMysql。具體操作如下:
在工程ConMysql上點(diǎn)擊右鍵
properties->Java Build Path->Libraries
點(diǎn)擊Add External JARS...
選取解壓后的mysql-connector-java-XX.XX.XX-bin.jar
2)驅(qū)動(dòng)已經(jīng)導(dǎo)入,下面我們來寫一個(gè)程序驗(yàn)證一下
package test1;import java.sql.*;public class MysqlJdbc { public static void main(String args[]) { try {
Class.forName("com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅(qū)動(dòng)程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
} catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
} try {
Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test1","root","Aa-12345"); //連接URL為 jdbc:mysql//服務(wù)器地址/數(shù)據(jù)庫名 ,后面的2個(gè)參數(shù)分別是登陸用戶名和密碼
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu"); //user 為你表的名稱while (rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
點(diǎn)擊運(yùn)行程序:出現(xiàn)以下代碼則表示連接成功
Success loading Mysql Driver!
Success connect Mysql server!
npu(mysql里自己寫入的內(nèi)容)
以上就是esclipse連接mysql數(shù)據(jù)庫的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識。