JDBC开发步骤:
1.注册 驱动
2.获得连接
3.获得语句执行平台
4.执行SQL语句
5.处理结果
6.释放资源
public static void main(String[] args)throws ClassNotFoundException, SQLException {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库连接
String url ="jdbc:mysql://localhost:3306/crm";
String username="root";
String password="root";
Connection con = DriverManager.getConnection(url,username,password);
System.out.println(con);
//3.获得语句执行平台
// Statement stat=con.createStatement();
//4. 执行sql语句
String sql ="SELECT * FROM sort";
// String sql= "SELECT * FROM sort";
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()){
System.out.println(rs.getInt("sid")+" ");
}
//释放资源
rs.close();
pst.close();
con.close();
System.out.println(rs);
}
public static void main(String[] args)throws Exception {
Connection con = JDBCUtils.getConnection();
PreparedStatement pst = con.prepareStatement("SELECT * FROM sort");
ResultSet rs = pst.executeQuery();
//创建集合对象
List list =new ArrayList<>();
while (rs.next()){
Sort s =new Sort(rs.getInt("sid"),
rs.getString("sname"),
rs.getDouble("sprice"),
rs.getString("sdesc"));
list.add(s);
}
for (Sort s : list){
System.out.println(s);
}
JDBCUtils.close(con,pst,rs);
}
我是一个自学Android和JavaWeb的菜鸟。如果可以Flower一下我的GitHub。感激不尽
https://github.com/FoxconnPeter
网友评论