1. Insert
Class UtiDb
package JavaJDBC.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class UtiDb {
// MySQL url address
private static String urlJdb = "jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf8";
// MySQL user name
private static String namUseDb = "root";
// MySQL user password
private static String pasUseDb = "ccmj";
// MySQL driver name
private static String namDriDb = "com.mysql.jdbc.Driver";
/**
* Get connection
* @author Linux_ccmj
* @date 16-5-10 下午7:12
* @method getConFun()
* @parameters
* @return Connection
* @throws ClassNotFoundException, SQLException
*/
public Connection getConFun() throws ClassNotFoundException, SQLException {
Class.forName(namDriDb);
Connection con = DriverManager.getConnection(urlJdb, namUseDb, pasUseDb);
return con;
}
/**
* Close connection
* @author Linux_ccmj
* @date 16-5-10 下午7:16
* @method clsConFun
* @parameters Connection
* @return
* @throws SQLException
*/
public void clsConFun(Statement sta, Connection con) throws SQLException {
if (sta!=null){
if (con!=null){
con.close();
}
}
}
}
Class Demo1
package JavaJDBC.Chap04.sec02;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* Created by linux_ccmj on 16-5-14.
*/
public class Demo1 {
// utiDb
private static UtiDb utiDb = new UtiDb();
// Add book fun
/**
* Insert book into t_book2
* @param boo
* @return
* @throws Exception
*/
private static int addBooFun(Book boo) throws Exception{
// Connection, preparedstatement
Connection con = utiDb.getConFun();
PreparedStatement staPre = con.prepareStatement("INSERT INTO t_book2 VALUES (NULL, ?, ?, ?, ?)");
// Execute
staPre.setString(1, boo.getNamBoo());
staPre.setFloat(2, boo.getPriBoo());
staPre.setString(3, boo.getNamAut());
staPre.setInt(4, boo.getIdTypBoo());
int marRes = staPre.executeUpdate();
// Close
utiDb.clsConFun(staPre, con);
// Return
return marRes;
}
// Test main
public static void main(String[] args) throws Exception {
// Create book
Book boo = new Book("军刀", 35, "娱乐工坊", 2);
// Add book and display message
int marRes = addBooFun(boo);
if (marRes==1){
System.out.println("成功增加一组数据!");
}
else {
System.out.println("未增加数据!");
}
}
}
2. Update
Class UtiDb
Class Book
Class Demo1 --> Update
package JavaJDBC.Chap04.sec03;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
* Created by linux_ccmj on 16-5-15.
*/
public class Demo01 {
// utiDb
private static UtiDb utiDb = new UtiDb();
// Update book fun
/**
* Update book fun
* @param boo
* @return
* @throws Exception
*/
private static int updBooFun(Book boo) throws Exception{
// Connection, string command, preparedstatement
Connection con = utiDb.getConFun();
String strComSql = "update t_book2 set bookName=?, price=?, author=?, bookTypeId=? where id=?";
PreparedStatement staPre = con.prepareStatement(strComSql);
staPre.setString(1, boo.getNamBoo());
staPre.setFloat(2, boo.getPriBoo());
staPre.setString(3, boo.getNamAut());
staPre.setInt(4, boo.getIdTypBoo());
staPre.setInt(5, boo.getIdBoo());
// Execute, result mark
int marRes = staPre.executeUpdate();
// Close
utiDb.clsConFun(staPre, con);
// Return
return marRes;
}
// Test main
public static void main(String[] args) throws Exception {
// Book
Book boo = new Book(11, "驻京办主任", 77, "王晓芳", 2);
// Update book
int marRes = updBooFun(boo);
// Display result
if (marRes==1){
System.out.println("数据更新成功!");
}
else {
System.out.println("数据更新失败!");
}
}
}