標籤:io ar os java for on 資料 cti ad
1、安裝mysql資料庫(具體方法不贅述了);登陸mysql語句:mysql -h localhost -u root -p 1905;
2、下載jdbc;
3、開啟eclipse之後,選擇工程,將jdbc的jar包匯入;
4、寫一個程式驗證下:
package webJDBC;
import java.sql.*;
public class MysqlJdbc {
public static void main(String[] args){
//載入jdbcDriver
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Success loading Mysql Driver!");
} catch (Exception e) {
System.out.println("Error!");
e.printStackTrace();
}
try{
//連結資料庫
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/SY","root","1905");
System.out.println("success connect Mysql Server!");
Statement stmt = connect.createStatement();
//插入2條資料
int num =2;
PreparedStatement statement = connect.prepareStatement("insert into user values(?,?)");
for(int i=0;i<num;i++){
statement.setString(1, "zhaopeng"+i);
statement.setString(2, "mima"+i);
statement.executeUpdate();
}
ResultSet rs = stmt.executeQuery("select * from user");
while(rs.next()){
System.out.println(rs.getString("name"));
}
}catch(Exception e){
System.out.println("get data error!");
e.printStackTrace();
}
}
}
Eclipse串連Mysql資料庫