增 String sql= "insert into d_book values(?,?,?,?,?)";//预编译
private static dbutil db=new dbutil();
private static int addBook(bookModel bookmodel1) throws Exception{
Connection con=db.getCon();
String sql="insert into book1 values(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, bookmodel1.getId());
pstmt.setString(2,bookmodel1.getBookName());
pstmt.setFloat(3,bookmodel1.getBookPrice());
pstmt.setString(4,bookmodel1.getAuthor());
pstmt.setInt(5,bookmodel1.getNumber());
int result =pstmt.executeUpdate();
db.close(pstmt, con);
return result;
}
删 String sql= "delete from d_book where id=? ";
private static dbutil db=new dbutil();
private static int DeleteBook(int id) throws Exception{
Connection con=db.getCon();
String sql="delete from book1 where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1,id);//直接写 id
int result=pstmt.executeUpdate();
db.close(pstmt, con);
return result;
}
改 String sql= "update d_book set bookName=?,bookPrice=?,author=?,number=?";
private static dbutil db=new dbutil();
private static int updateBook(bookModel book1)throws Exception{
Connection con=db.getCon();
String sql="update book1
set bookName=?,bookPrice=?,author=?,number=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book1.getBookName());
pstmt.setFloat(2, book1.getBookPrice());
pstmt.setString(3, book1.getAuthor());
pstmt.setInt(4, book1.getNumber());
pstmt.setInt(5,book1.getId());
int result=pstmt.executeUpdate();
db.close(pstmt, con);
return result;
}
查 String sql="select * from d_book(where id= ?)";
private static dbutil db=new dbutil();
private static void ListBook() throws Exception{
Connection con=db.getCon();
String sql="select * from book1";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();//结果集
while(rs.next())
}
private static dbutil db=new dbutil();
private static int addBook(bookModel bookmodel1) throws Exception{
Connection con=db.getCon();
String sql="insert into book1 values(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, bookmodel1.getId());
pstmt.setString(2,bookmodel1.getBookName());
pstmt.setFloat(3,bookmodel1.getBookPrice());
pstmt.setString(4,bookmodel1.getAuthor());
pstmt.setInt(5,bookmodel1.getNumber());
int result =pstmt.executeUpdate();
db.close(pstmt, con);
return result;
}
删 String sql= "delete from d_book where id=? ";
private static dbutil db=new dbutil();
private static int DeleteBook(int id) throws Exception{
Connection con=db.getCon();
String sql="delete from book1 where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1,id);//直接写 id
int result=pstmt.executeUpdate();
db.close(pstmt, con);
return result;
}
改 String sql= "update d_book set bookName=?,bookPrice=?,author=?,number=?";
private static dbutil db=new dbutil();
private static int updateBook(bookModel book1)throws Exception{
Connection con=db.getCon();
String sql="update book1
set bookName=?,bookPrice=?,author=?,number=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book1.getBookName());
pstmt.setFloat(2, book1.getBookPrice());
pstmt.setString(3, book1.getAuthor());
pstmt.setInt(4, book1.getNumber());
pstmt.setInt(5,book1.getId());
int result=pstmt.executeUpdate();
db.close(pstmt, con);
return result;
}
查 String sql="select * from d_book(where id= ?)";
private static dbutil db=new dbutil();
private static void ListBook() throws Exception{
Connection con=db.getCon();
String sql="select * from book1";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();//结果集
while(rs.next())
{
int id=rs.getInt("id");
String bookName=rs.getString("bookName");
Float bookPrice=rs.getFloat("bookPrice");
String author=rs.getString("author");
int number=rs.getInt("number");
}
}
boolean next()
throws SQLException将光标从当前位置向前移一行。ResultSet 光标最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;第二次调用使第二行成为当前行,依此类推。当调用 next 方法返回 false 时,光标位于最后一行的后面。任何要求当前行的 ResultSet 方法调用将导致抛出 SQLException。如果结果集的类型是TYPE_FORWARD_ONLY,则其 JDBC 驱动程序实现对后续 next 调用是返回 false 还是抛出 SQLException 将由供应商指定。 如果对当前行开启了输入流,则调用 next 方法将隐式关闭它。读取新行时,将清除 ResultSet 对象的警告链。
返回:
如果新的当前行有效,则返回 true;如果不存在下一行,则返回 false