jdbc使用 PreparedStatement 接口实现添加数据操作

来源:互联网 发布:淘宝设计工资一般多少 编辑:程序博客网 时间:2024/06/06 01:25

jdbc使用 PreparedStatement  接口实现添加数据操作


package model;


public class Book {
private int id;
private String bookName;
private float price;
private String author;
private int bookTypeId;



public Book(int id, String bookName, float price, String author, int bookTypeId) {
super();
this.id = id;
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
}
public Book(String bookName, float price, String author, int bookTypeId) {
super();
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(int bookTypeId) {
this.bookTypeId = bookTypeId;
}

}


package util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;


public class DbUtil {


private static String url="jdbc:mysql://localhost:3306/db_book";
private static String user="root";
private static String password="root";
private static String mysql_driver="com.mysql.jdbc.Driver";
public Connection getCon(){
Connection con=null;
try {
Class.forName(mysql_driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();  
}
return con;
}
public void closeCon(Statement s,Connection con) throws SQLException{
if(s!=null){
s.close();
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void closeCon(PreparedStatement s,Connection con) throws SQLException{
if(s!=null){
s.close();
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



}


package chap04.sec02;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


import model.Book;
import util.DbUtil;


public class Demo1 {
private static DbUtil dbUtil=new DbUtil();
private static int addBook(Book book) throws SQLException{

String sql="insert into t_book values(null,?,?,?,?)"; 
Connection con= dbUtil.getCon();
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getBookName());
pstmt.setFloat(2, book.getPrice());
pstmt.setString(3, book.getAuthor());
pstmt.setInt(4, book.getBookTypeId());
int result=pstmt.executeUpdate();
dbUtil.closeCon(pstmt, con);
return result;
}


public static void main(String[] args) {
// TODO Auto-generated method stub
Book book=new Book("javaqwe4",444,"李四4",4);
try {
int result =addBook(book);
if(result==1){
System.out.println("添加成功");
}
else{
System.out.println("添加失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

1 0
原创粉丝点击