package dbConnPackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnToMySql {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Loading mySql-connector-java.jar successed!");
} catch (ClassNotFoundException e1) {
//System.out.println("Cann't find mySql-connector-java.jar!");
e1.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/student";
Connection conn;
try {
conn = DriverManager.getConnection(url, "root", "3312");
Statement stmt = conn.createStatement();
//System.out.println("mySqlDB Connected!");
String updateSql = "update studentInfo set Telephone = ? , StuName = ? where StuID = ?";
PreparedStatement pst = conn.prepareStatement(updateSql);
pst.setString(1, "133556565");
pst.setString(2, "Alice");
pst.setString(3, "20120002");
pst.executeUpdate();
String delSql = "delete from studentInfo where StuID = ?";
pst = conn.prepareStatement(delSql);
pst.setString(1, "20120001");
pst.executeUpdate();
String insertSql = "insert into studentInfo values(?, ?, ?)";
pst = conn.prepareStatement(insertSql);
pst.setString(1, "20120004");
pst.setString(2,"Bob");
pst.setString(3,"189111");
pst.executeUpdate();
String selectSql = "select * from studentInfo";
ResultSet rs = stmt.executeQuery(selectSql);
System.out.println("StuID\t" + "StuName\t" + "Telephone");
while (rs.next()) {
System.out.print(rs.getString(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.println(rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库操作 Java视频大讲堂 视频教程
http://school.itzcn.com/video-vid-2308-spid-50.html
http://school.itzcn.com/video-vid-2309-spid-50.html
http://school.itzcn.com/video-vid-2310-spid-50.html