Java笔记-JDBC
public class testJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//创建链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ceshi","root","mysql");
//获取执行对象
Statement stat = conn.createStatement();
String sql = "select * from question_detail";
ResultSet set = stat.executeQuery(sql);
while(set.next()){
System.out.println(set.getObject("id") + "--" +set.getObject("question_id"));
}
if(set != null){
set.close();
}
if(stat != null){
stat.close();
}
if(conn != null){
conn.close();
}
}
}
//预编译
//JDBC预编译
public class testJDBCPre {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ceshi","root","mysql");
String sql = "insert question_detail values(?,?,?)";
//创建执行对象
PreparedStatement prestat = conn.prepareStatement(sql);
prestat.setInt(1,7);
prestat.setInt(2,114);
prestat.setString(3,"easyy");
int result = prestat.executeUpdate();
System.out.println("执行结果:"+ result);
if(prestat != null){
prestat.close();
}
if(conn != null){
conn.close();
}
}
}