jdbc写入和读取过程

[jdbc写操作]

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/big","root","root");

ppst = conn.preparedStatement("insert into test(id,name,age) values(?,?,?)");

//绑定参数

ppst.setInteger(1,1);

ppst.setString(2,"tom");

ppst.setInteger(3,"tom");

ppst.executeUpdate();

ppst.close();

ppst.close();

 

[jdbc读操作]

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/big4","root","root");

ppst =conn.PreparedStatement("select id,name from test");

ResultSet rs = ppst.executeQuery();

while(rs.next()){

  int id = rs.getInt();

  String name = rs.getString("name");

}

  rs.close();

  conn.close();

posted @ 2018-07-06 08:35  shizeqi  阅读(455)  评论(0编辑  收藏  举报