Eclipseè¿æ¥MySQLæ°æ®åºéè¦é ç½®æ°æ®æºæ¥è¿æ¥ã
1ãæå¼Eclipseï¼å建ä¸ä¸ªé¡¹ç®ï¼testConnï¼ï¼
æä½ï¼å³é®ç¹å»my--->build Path--->add external Archiver...éæ©jdbc驱å¨ï¼ç¹å»ç¡®å®ã
2ã项ç®å表ä¸æ·»å çlibï¼
3ãåjdbcè¿æ¥ç±»éªè¯ï¼
import java.sql.*;
publicclass MysqlJdbc {
publicstaticvoid main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); //å è½½MYSQL JDBC驱å¨ç¨åº
//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/test","root","198876");
//è¿æ¥URL为 jdbc:mysql//æå¡å¨å°å/æ°æ®åºå ï¼åé¢ç2个åæ°åå«æ¯ç»éç¨æ·ååå¯ç
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
//user ä¸ºä½ è¡¨çå称
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}