CRUD 增删改查——Statement
CRUD 增删改查——PrepareStatement
一、CRUD 增删改查——Statement
public class MainText {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/数据库名?useSSL=false", "用户名", "密码");
Statement statement = conn.createStatement();
String sql = "select * from teacher where name = '陈老师'";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("name")+rs.getInt("phone"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、CRUD 增删改查——PrepareStatement
public class MainText {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/数据库名?useSSL=false", "用户名", "密码");
String sql = "select * from teacher where name = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "陈老师");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
System.out.println(rs.getString("name")+" "+rs.getInt("phone"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
正在尝试写博客,把会的分享给你们,如有写的不好的地方,希望指点一下,喜欢的朋友们请点个赞,谢谢!