您的位置:首页 > 数据库

jdbc使用 Statement 接口实现添加数据操作(使用面向对象和数据库连接工具类)

2016-06-24 20:33 585 查看
jdbc使用 Statement  接口实现添加数据操作(使用面向对象和数据库连接工具类)

package model;

public class Book {
private int id;
private String bookName;
private float price;
private String author;
private int 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;
4000

import java.sql.DriverManager;

import java.sql.SQLException;

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(Connection con){
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

package chap03.sec02;

import java.sql.Connection;

import java.sql.SQLException;

import model.Book;

import util.DbUtil;

public class Demo2 {
public static int add(String bookName,float price,String author,int bookTypeId) throws SQLException{
String sql="insert into t_book values(null,'"+bookName+"',"+price+",'"+author+"',"+bookTypeId+")";
Connection con=new DbUtil().getCon();
int result=con.createStatement().executeUpdate(sql);
return result;

}

public static int add2(Book book) throws SQLException{
String sql="insert into t_book values(null,'"+book.getBookName()+"',"+book.getPrice()+",'"+book.getAuthor()+"',"+book.getBookTypeId()+")";
Connection con=new DbUtil().getCon();
int result=con.createStatement().executeUpdate(sql);
return result;
}

public static void main(String[] args) {
/*try {
int result = add("javaqwe",121,"李四",1);
if(result==1){
System.out.println("插入成功");
}
else{
System.out.println("插入失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
Book book=new Book("javaqwe2",1212,"李四2",2);
try {
int result =add2(book);
if(result==1){
System.out.println("插入成功");
}
else{
System.out.println("插入失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: