我也刚学不久,先给个老师课堂案例,简单的几部
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gjp","root","meng");
//3.获取Statement
Statement stmt = conn.createStatement();
//4.获取结果集
String sql = "select * from gjp_ledger " +
"where createtime between '2017+5-03-01' " +
"and '2015-04-30'";
// System.out.println(sql);
ResultSet rs = stmt.executeQuery(sql);//查询出gjp_ledger表中所有2015年3月份和4月份的数据
//5.遍历结果集
while(rs.next()){
System.out.println(rs.getInt(1) + "\t" +
rs.getString(2) + "\t" +
rs.getDouble(3) + "\t" +
rs.getInt(4) + "\t" +
rs.getString(5) + "\t" +
rs.getString(6) + "\t" +
rs.getString(7));
}
//6.释放资源
rs.close();
stmt.close();
conn.close(); |