//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(); } }
事务是并发控制的单位,是用户自定义的一个操做序列,这些操做要么都作,要么都不作,是一个不可分割的工做单位。当须要对数据库进行统一的提交和回滚时,好比当进行转帐时,必须帐户两边更改数据,要么成功要么失败,若是某一操做出错,则回滚以前全部的操做。数组