public class PreparedStatmentDemo { public static void main(String[] args) { try { //var:定义变量 var c =new DbUtil().getConn(); PreparedStatement ps = c.prepareStatement("select *from stu"); ResultSet rs =ps.executeQuery(); while(rs.next()){ System.out.print(rs.getInt("id")); System.out.print(","); System.out.print(rs.getString("name")); System.out.println(rs.getString("score")); //时间 // Date bir = rs.getDate("birth"); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // System.out.println(sdf.format(bir)); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }
链接数据库类
private String driver = "com.mysql.cj.jdbc.Driver"; private String url = "jdbc:mysql:/db1"; private String user = "aa"; private String password = ""; private Connection conn = null;//Connection链接语句 public DbUtil() { try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } }
原文:https://www.cnblogs.com/Gu1015/p/14465224.html