//2015013 public static int insert(Student stu) throws SQLException{ conn = DriverManager .getConnection(url,userName,password); //sql語句使用拼接的方式 String strsql="insert into student(stuno,name,age,birthdate)" + " values('"+stu.getStuno()+"','"+stu.getName()+"',"+stu.getAge()+",'"+stu.getBirthdate()+"')"; System.out.println(strsql); st=conn.createStatement(); resultNum=st.executeUpdate(strsql); return resultNum; }
運行結果:
java
//2015013 String sql="select * from student where name=?"; pst=conn.prepareStatement(sql); pst.setString(1, name); rs=pst.executeQuery(); Student student=null; //while(rs.next())必須寫,不然出現Before start of result set,由於rs是鏈式存儲,一開始指針不在第一個數據位置,因此必須next()才能取得數據 while(rs.next()){ student=new Student(rs.getString("stuno"),rs.getString("name"),rs.getInt("age"),rs.getDate("birthdate")); } ... String sql="select * from student where birthdate between ? and ?"; pst=conn.prepareStatement(sql); pst.setDate(1, java.sql.Date.valueOf(begin)); pst.setDate(2, java.sql.Date.valueOf(end)); ...
運行結果:
mysql
//2015013 public void batchTest() throws ClassNotFoundException { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = DriverManager.getConnection(url, userName, password); con.setAutoCommit(false); pstmt = con.prepareStatement(strSql); for (int i = 0; i < 1000; i++) { pstmt.setString(1, "2015022"); pstmt.setString(2, "郭雅清"); pstmt.setInt(3, 11); pstmt.setDate(4, java.sql.Date.valueOf("2000-09-02")); pstmt.addBatch(); } pstmt.executeBatch(); con.commit(); } catch (SQLException sqlE) { sqlE.printStackTrace(); } finally { realeaseAll(rs,pst,conn); }
//2015013 @Override public int add(Student stu) { // TODO Auto-generated method stub Connection conn=null; PreparedStatement pst=null; String sql="insert into students(name) values(?)"; int result=1; try { conn=JDBCUtil.getConnection(); pst=conn.prepareStatement(sql); pst.setString(1, stu.getName()); result=pst.executeUpdate(); if(result<0){ result=-1; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JDBCUtil.realeaseAll(null, pst, conn); } return result; } ... public Map<String,Student> fun(List<Student> stulist){ Map<String,Student> map=new HashMap<String,Student>(); for (int i = 0; i < stulist.size(); i++) { map.put(stulist.get(i).getName(),stulist.get(i)); } return map; }
運行結果:
sql
界面:
管理員(插入、刪除):
用戶(讀取):
數據庫
//2015013 public static void transfer(Account a, Account b, double x) throws Exception { String sql1="update account set balance=? where name=?"; String sql2="update account set balance=? where name=?"; try { conn=DriverManager.getConnection(url, userName, password); conn.setAutoCommit(false); if(a.getBalance()>=x){ pst = conn.prepareStatement(sql1); pst.setDouble(1, a.getBalance()-x); pst.setString(2, a.getName()); pst.executeUpdate(); pst = conn.prepareStatement(sql2); pst.setDouble(1, b.getBalance()+x); pst.setString(2, b.getName()); pst.executeUpdate(); conn.commit(); System.out.println("轉帳成功"); }else{ throw new SQLException("餘額不足"); } }catch (SQLException e){ e.printStackTrace(); if (conn != null) { try{ System.err.print("事務正在回滾"); conn.rollback(); } catch(SQLException excep) { excep.printStackTrace(); } } } finally { pst.close(); conn.close(); } }
事務是併發控制的單位,是用戶自定義的一個操做序列,這些操做要麼都作,要麼都不作,是一個不可分割的工做單位。當須要對數據庫進行統一的提交和回滾時,好比當進行轉帳時,必須帳戶兩邊更改數據,要麼成功要麼失敗,若是某一操做出錯,則回滾以前全部的操做。數組