不多bb了直接上。 工具:myeclipse 2016,mysql 5.7 目的:java操作資料庫增刪改查商品信息 test資料庫的goods表 gid主鍵,自增 1、實體類Goods:封裝資料庫數據(與資料庫表中各欄位相匹配的類) 2、實現類GoodsDao(不帶DBHelper):操作資料庫實 ...
不多bb了直接上。
工具:myeclipse 2016,mysql 5.7
目的:java操作資料庫增刪改查商品信息
test資料庫的goods表
gid主鍵,自增
1、實體類Goods:封裝資料庫數據(與資料庫表中各欄位相匹配的類)
// 實體類 public class Goods { private int gid; private String gname; private double gprice; private String gdate; //生成get、set方法 public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public double getGprice() { return gprice; } public void setGprice(double gprice) { this.gprice = gprice; } public String getGdate() { return gdate; } public void setGdate(String gdate) { this.gdate = gdate; } //生成構造方法 public Goods(int gid, String gname, double gprice, String gdate) { super(); this.gid = gid; this.gname = gname; this.gprice = gprice; this.gdate = gdate; } //生成無參構造方法 public Goods() { super(); } }
2、實現類GoodsDao(不帶DBHelper):操作資料庫實現增刪改查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class GoodsDao { public static void main(String[] args) { GoodsDao dao = new GoodsDao(); // dao.add(); // dao.del(); // dao.upd(); Scanner input = new Scanner(System.in); System.out.println("請輸入商品名稱:"); String name = input.next(); System.out.println("請輸入商品價格:"); double price = input.nextDouble(); dao.newAdd(name, price); // System.out.println("請輸入最低價格:"); // double price = input.nextDouble(); // // dao.search(price); // System.out.println("已經成功完成操作!"); } // 增數據 public void add(){ try{ // 1.通過反射,載入驅動類到jvm Class.forName("com.mysql.jdbc.Driver"); // 2.獲取資料庫連接對象 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true","root",""); // 3.創建資料庫操作對象 Statement stmt = conn.createStatement(); // 4.操作資料庫 stmt.execute("insert into goods(gname,gprice,gdate) values('ggg','2.33',now())"); //5.關閉各個資源 stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } //刪數據 public void del(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true","root",""); Statement stmt = conn.createStatement(); stmt.execute("delete from goods where gid=1"); stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } //改數據 public void upd(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true","root",""); Statement stmt = conn.createStatement(); stmt.execute("update goods set gname='ggg' where gid=1 "); stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } //查數據 public void search(double price){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true","root",""); Statement stmt = conn.createStatement(); // 結果集對象 ResultSet rs = stmt.executeQuery("select * from goods where gprice>"+price); while(rs.next()){ System.out.println(rs.getString(1)+"#"+rs.getString("gname")+"#"+rs.getDouble("gprice")+"#"+rs.getString("gdate")); } rs.close(); stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } //用拼字元串的方法增數據 public void newAdd(String gname, double gprice){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true","root",""); // Statement stmt = conn.createStatement(); // // stmt.execute("insert into goods(gname, gprice,gdate) values('"+gname+"','"+gprice+"',now())"); // // stmt.close(); //拼字元串更簡單 String sql = "insert into goods(gname, gprice, gdate) values(?,?,now())"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, gname); pstmt.setDouble(2, gprice); pstmt.execute(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } }
3、DBHelper類:解決上述代碼操作資料庫的重覆工作
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; /** * 獲取資料庫操作的連接對象 * 關閉資料庫操作的各種資源 * @author 晏先政 * */ public class DBHelper { private static final String className = "com.mysql.jdbc.Driver"; private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true"; private static final String uname = "root"; private static final String upass = ""; /** * 獲取資料庫連接對象的方法 */ public static Connection getConn(){ Connection conn = null; try{ Class.forName(className); conn = DriverManager.getConnection(url,uname, upass); } catch(Exception e){ e.printStackTrace(); } return conn; } /** * 關閉資料庫連接對象 */ public static void closeConn(Connection conn){ try{ if(conn!=null){ conn.close(); } } catch(Exception e){ e.printStackTrace(); } } /** * 關閉資料庫操作對象 */ public static void closeStmt(Statement stmt){ try{ if(stmt!=null){ stmt.close(); } } catch(Exception e){ e.printStackTrace(); } } /** * 關閉資料庫操作對象 */ public static void closePstmt(PreparedStatement pstmt){ try{ if(pstmt!=null){ pstmt.close(); } } catch(Exception e){ e.printStackTrace(); } } /** * 關閉資料庫操作對象 */ public static void closeRs(ResultSet rs){ try{ if(rs!=null){ rs.close(); } } catch(Exception e){ e.printStackTrace(); } } }
4、實現類NewDao(帶DBHelper):操作資料庫實現增刪改查
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class NewDao { public static void main(String[] args) { NewDao nd = new NewDao(); nd.show(); // Scanner input = new Scanner(System.in); //改數據 // System.out.println("請輸入要修改的商品編號:"); // int gid = input.nextInt(); // System.out.println("請輸入要修改的商品名稱:"); // String gname = input.next(); // System.out.println("請輸入要修改的商品價格:"); // double gprice = input.nextDouble(); // // nd.upd(gid, gname, gprice); //查數據 // System.out.println("請輸入最低價格:"); // double mprice = input.nextDouble(); // System.out.println("請輸入最高價格:"); // double xprice = input.nextDouble(); // // nd.search(mprice, xprice); } private Connection conn = null; private PreparedStatement pstmt = null; private ResultSet rs = null; //查數據 public void search(double minprice, double maxprice){ try{ conn = DBHelper.getConn(); String sql = "select * from goods where gprice>=? and gprice<=?"; // 預編譯的對象 pstmt = conn.prepareStatement(sql); pstmt.setDouble(1, minprice); pstmt.setDouble(2, maxprice); rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString("gid")+"#"+rs.getString("gname")+"#"+rs.getString("gprice")); } } catch(Exception e){ e.printStackTrace(); } finally{ DBHelper.closeRs(rs); DBHelper.closePstmt(pstmt); DBHelper.closeConn(conn); } } //改數據 public void upd(int gid , String gname, double gprice){ try{ conn = DBHelper.getConn(); String sql = "update goods set gname=?, gprice=? where gid=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, gname); pstmt.setDouble(2, gprice); pstmt.setInt(3, gid); pstmt.execute(); } catch(Exception e){ e.printStackTrace(); } finally{ DBHelper.closePstmt(pstmt); DBHelper.closeConn(conn); } } //用集合展示數據 public List<Goods> getAllGoods(){ List<Goods> list = new ArrayList<Goods>(); try{ conn = DBHelper.getConn(); String sql = "select * from goods"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ Goods goods = new Goods(rs.getInt("gid"),rs.getString("gname"),rs.getDouble("gprice"),rs.getString("gdate")); list.add(goods); } } catch(Exception e){ e.printStackTrace(); } finally{ DBHelper.closeRs(rs); DBHelper.closePstmt(pstmt); DBHelper.closeConn(conn); } return list; } public void show(){ List<Goods> list = getAllGoods(); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).getGid()+"#"+list.get(i).getGname()+"#"+list.get(i).getGprice()); } } }