ç¨eclipseè¿æ¥mysqlæ°æ®åºçæ¹æ³ï¼
1ãæå¼Eclipseï¼å建ä¸ä¸ªé¡¹ç®ï¼myDemoï¼ï¼
æä½ï¼å³é®ç¹å»my--->build Path--->add external Archiver...éæ©jdbc驱å¨ï¼ç¹å»ç¡®å®ã
2ãå½å项ç®å表ï¼
3ã驱å¨å·²ç»å¯¼å ¥ï¼ä¸é¢æ¥åä¸ä¸ªç¨åºéªè¯ä¸ä¸ï¼
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();
}
}
}
4ãç¹å»è¿è¡ç¨åºï¼ãã
Success loading Mysql Driver!
Success connect Mysql server!
huzhihengãã
åºç°ä¸é¢ç»æï¼è¯´æè¿æ¥æ°æ®åºæåã