JDBC的連結及封裝,JDBC連結封裝
匯入 mysql 的jar包jar包:可以直接拿來用,又不想我們看到原始碼 sql語句 一定注意:當update,delete時 一定注意where 條件,一定要寫!!!
public static void add() { // try catch 捕獲異常,try有異常會跳到catch Connection con = null; try { // 1.選擇要串連哪一種資料庫---JDBC載入資料庫驅動 Class.forName("com.mysql.jdbc.Driver"); // 2.建立連結(主機名稱,連接埠號碼,使用者名稱,密碼)---url包含主機名稱+連接埠號碼+資料庫 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "123456"); // 3.選擇操作的資料庫--此動作在上一步的url中整合 // 4.建立命令視窗 寫 sql語句 PreparedStatement pstmt = con.prepareStatement("insert into zhuce (name,password,sex,id) value(?,?,?,?)"); pstmt.setString(1, "8888"); pstmt.setString(2, "8888"); pstmt.setString(3, "F"); pstmt.setString(4, "8888"); // 5.運行sql語句 ,查看結果---增刪改調用executeUpdate 返回受影響的行數;查詢調用 executeQuery // 返回查詢結果集 int result = pstmt.executeUpdate(); System.out.println(result); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } }public static void main (String[] args){ add(); }
以上是add方法,update和delete方法只是改變了sql語句
update://根據主鍵查詢PreparedStatement pstmt = con.prepareStatement("update zhuce set sex=?,id=? where name=? and password=?");pstmt.setString(1, "M");pstmt.setString(2, "2222");pstmt.setString(3, "2222");pstmt.setString(4, "1111");delete://刪除主鍵PreparedStatement pstmt = con.prepareStatement("delete from zhuce where name=?and password=?");pstmt.setString(1, "8888");pstmt.setString(2, "8888");
JDBC的封裝
將1,2步 close sql語句的輸出 進行封裝
public class JdbcUtil { public static Connection getConnection(){ Connection con =null; try { //1.載入驅動 Class.forName("com.mysql.jdbc.Driver"); //2.建立串連 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "123456"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static void close(Connection con){ try { if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static int executeUpdate(String sql,Object[] params){ Connection con = getConnection(); int result =0; try { PreparedStatement pstmt = con.prepareStatement(sql); if(params!=null){ for(int i=0;i<params.length;i++){ pstmt.setObject(i+1, params[i]); } } result = pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { close(con); } return result; }}JAVABEAN又叫 bean 等跟資料庫裡面的表發生映射
package com.neuedu.bean;import com.sun.org.apache.xml.internal.security.Init;public class Student { private Integer sid; private String sname; private Integer age; private Integer sex; //這四句代碼就是javabean public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Student(Integer sid, String sname, Integer age, Integer sex) { super(); this.sid = sid; this.sname = sname; this.age = age; this.sex = sex; } public Student() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age + ", sex=" + sex + "]"; }}簡化使用動態數組//一個方法中只能有一個動態參數//動態參數必須位於參數列表的最後一個將之前object[] 改成 動態數組 object...
public static int executeUpdate(String sql,Object... params)
增刪改 方法簡化
public static int update(Student student){ return JdbcUtil.executeUpdate("update student set sname=?,age=?,sex=? where sid=?",student.getSname(),student.getAge(),student.getSex(),student.getSid()); }public static int add(Student student) { return JdbcUtil.executeUpdate("insert into student (sid,sname,age,sex) values(?,?,?,?)", student.getSid(),student.getSname(),student.getAge(),student.getSex()); }public static int delete(int id){ return JdbcUtil.executeUpdate("delete from student where sid=?", id); }
查詢不適合用數組,因為不知道有多少資料;用集合,集合有兩種 ArrayList,LinkedList基於ArrayList(Vector,ArrayList)適合查詢,而LinkedList(鏈表)適合添加,刪除操作。用到 遊標 的操作:rs.next() ,判斷有沒有下一行資料,有的話 遊標推到下一行,並返回true ;反之 返回false 。 首先從最上面開始第一次調用rs.next() 就會調用首行的下一行,也就是第一行資料
public static List<Student> getStudents(){ Connection con = null; List<Student> list = new ArrayList<>(); try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "123456"); PreparedStatement pstmt = con.prepareStatement("select * from student"); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ Student student=new Student(); student.setSid(rs.getInt("sid")); student.setSname(rs.getString("sname")); student.setSname(rs.getString("age")); student.setSname(rs.getString("sex")); list.add(student); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(con!=null){ con.close(); } } catch (Exception e2) { // TODO: handle exception } } return list; }}public static void main (String[] args){ List<Student> students=getStudents(); System.out.println(students);}