1. ExecuteUpdate Insert 1
i. 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();
}
}
}
}
ii. Class Demo1
package JavaJDBC.Chap03.sec02;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class Demo01 {
// Test main
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// MySQL command
String strComSql = "insert into t_book2 values(null, 'Idea Insert', 78, 'sen', 1)";
// Get connection, statement
UtiDb utiDb = new UtiDb();
Connection con = utiDb.getConFun();
Statement sta = con.createStatement();
// Statement execute, and verify
int resStaExe = sta.executeUpdate(strComSql);
System.out.println("插入表t_book2 " + resStaExe + " 条数据!");
// Close statement, connection
sta.close();
con.close();
}
}
2. ExecuteUpdate Insert 2
Class UtiDb
Class Demo2
package JavaJDBC.Chap03.sec02;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.Statement;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class Demo02 {
// UtiDb create
private static UtiDb utiDb = new UtiDb();
// Add book function
public static int addBooFun(String namBoo, float priBoo, String namAut, int numBooTyp) throws Exception{
// Get connection
Connection con = utiDb.getConFun();
// Get Statement, execute, verify
Statement sta = con.createStatement();
int numRes = sta.executeUpdate("insert into t_book2 values (null,'"+namBoo+"',"+priBoo+",'"+namAut
+"',"+numBooTyp+")");
// Close statement, connection
utiDb.clsConFun(sta, con);
return numRes;
}
// Test main
public static void main(String[] args) throws Exception{
// Add book
int numRes = addBooFun("Java Demo2", 888, "sen Demo2", 1);
if (numRes==1){
System.out.println("数据插入成功!");
}
else {
System.out.println("数据插入失败!");
}
}
}
3. ExecuteUpdate Insert 3
Class UtiDb
Class Book
package JavaJDBC.model;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class Book {
private int idBoo;
private String namBoo;
private float priBoo;
private String namAut;
private int idTypBoo;
public Book(int idBoo, String namBoo, float priBoo, String namAut, int idTypBoo) {
this.idBoo = idBoo;
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
}
public Book(String namBoo, float priBoo, String namAut, int idTypBoo) {
this.namBoo = namBoo;
this.priBoo = priBoo;
this.namAut = namAut;
this.idTypBoo = idTypBoo;
}
public int getIdBoo() {
return idBoo;
}
public void setIdBoo(int idBoo) {
this.idBoo = idBoo;
}
public String getNamBoo() {
return namBoo;
}
public void setNamBoo(String namBoo) {
this.namBoo = namBoo;
}
public float getPriBoo() {
return priBoo;
}
public void setPriBoo(float priBoo) {
this.priBoo = priBoo;
}
public String getNamAut() {
return namAut;
}
public void setNamAut(String namAut) {
this.namAut = namAut;
}
public int getIdTypBoo() {
return idTypBoo;
}
public void setIdTypBoo(int idTypBoo) {
this.idTypBoo = idTypBoo;
}
}
Class Demo3
package JavaJDBC.Chap03.sec02;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.Statement;
/**
* Created by linux_ccmj on 16-5-10.
*/
public class Demo03 {
// UtiDb create
private static UtiDb utiDb = new UtiDb();
// Add book function
/**
* Add book function
* @param boo
* @return
* @throws Exception
*/
public static int addBooFun(Book boo) throws Exception{
// Get connection
Connection con = utiDb.getConFun();
// Get Statement, execute, verify
Statement sta = con.createStatement();
int numRes = sta.executeUpdate("insert into t_book2 values (null,'"+boo.getNamBoo()+"',"+boo.getPriBoo()
+",'"+boo.getNamAut()+"',"+boo.getIdTypBoo()+")");
// Close statement, connection
utiDb.clsConFun(sta, con);
return numRes;
}
// Test main
public static void main(String[] args) throws Exception{
// Book create
Book boo = new Book("Demo3 Book Test", 333, "Demo3Test", 1);
// Add book
int marRes = addBooFun(boo);
if (marRes==1){
System.out.println("数据插入成功!");
}
else {
System.out.println("数据插入失败!");
}
}
}
4. ExecuteUpdate Update
Class UtiDb
Class Book
Class Demo1 --> Update
package JavaJDBC.Chap03.sec03;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.Statement;
/**
* Created by linux_ccmj on 16-5-11.
*/
public class Demo01 {
// UtiDb create
private static UtiDb utiDb = new UtiDb();
/**
* Update t_book2
* @param boo
* @return
* @throws Exception
*/
private static int updBooFun(Book boo) throws Exception {
// MySQL command
String comSql = "update t_book2 set bookName='"+boo.getNamBoo()+"', price="
+boo.getPriBoo()+", author='"+boo.getNamAut()+"', bookTypeId="
+boo.getIdTypBoo()+" where id=" +boo.getIdBoo();
// Connection, Statement
Connection con = utiDb.getConFun();
Statement sta = con.createStatement();
int marRes = sta.executeUpdate(comSql);
// Close statement, connection
utiDb.clsConFun(sta, con);
// Return result mark
return marRes;
}
// Test main
public static void main(String[] args) throws Exception {
// Book
Book boo = new Book(8,"Java Update Test", 144, "中文", 2);
// Update book
int marRes=updBooFun(boo);
if (marRes==1){
System.out.println("成功更新一组数据!");
}
else {
System.out.println("未更新数据!");
}
}
}
5. ExecuteUpdate Delete
Class UtiDb
Class Book
Class Demo1 --> Delete
package JavaJDBC.Chap03.sec04;
import JavaJDBC.model.Book;
import JavaJDBC.util.UtiDb;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.Struct;
/**
* Created by linux_ccmj on 16-5-14.
*/
public class Delete {
// UtiDb
private static UtiDb uitDb = new UtiDb();
// Delete fun
/**
* Delete book from t_book2
* @param id
* @return
* @throws Exception
*/
private static int delBooFun(int id) throws Exception{
// Connection, Statement,
Connection con = uitDb.getConFun();
Statement sta = con.createStatement();
String strComSql = "delete from t_book2 where id="+id;
// Execute
int marRes = sta.executeUpdate(strComSql);
System.out.println("删除表t_book2 " + marRes + " 条数据!");
// Close
uitDb.clsConFun(sta, con);
// Return
return marRes;
}
// Test main
public static void main(String[] args) throws Exception {
// Delete book
int marRes=delBooFun(9);
if (marRes==1){
System.out.println("成功更新一组数据!");
}
else {
System.out.println("未删除数据!");
}
}
}
Before:
After: