//添加user public void addUser(User user) throws Exception{ //通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接 Connection conn = null; DataSource ds = null; Context initCtx; try { initCtx = new InitialContext(); ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test"); if(ds!=null){ conn = ds.getConnection(); } } catch (NamingException e) { e.printStackTrace(); } String sql=""+ "insert into account"+ "(name,password)"+ "values("+ "?,?)"; //2.?通過(guò)數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù) PreparedStatement ptmt=conn.prepareStatement(sql); ptmt.setString(1,user.getName()); ptmt.setString(2,user.getPassword()); ptmt.execute(); //3.添加語(yǔ)句 //ResultSet rs=stmt.executeQuery("insert into account('name','password')value('wocao','12345')"); } //更改user public void updateUser(User user) throws SQLException{ //通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接 Connection conn = null; DataSource ds = null; Context initCtx; try { initCtx = new InitialContext(); ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test"); if(ds!=null){ conn = ds.getConnection(); } } catch (NamingException e) { e.printStackTrace(); } String sql=""+ " update account"+ " set name=?,password=? "+ " where id=? "; //2.?通過(guò)數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù) PreparedStatement ptmt=conn.prepareStatement(sql); ptmt.setString(1,user.getName()); ptmt.setString(2, user.getPassword()); ptmt.setInt(3,user.getId()); ptmt.execute(); } //刪除user public void delUser(Integer id) throws SQLException{ //通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接 Connection conn = null; DataSource ds = null; Context initCtx; try { initCtx = new InitialContext(); ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test"); if(ds!=null){ conn = ds.getConnection(); } } catch (NamingException e) { e.printStackTrace(); } String sql=""+ " delete from account "+ " where id=? "; //2.通過(guò)數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù) PreparedStatement ptmt=conn.prepareStatement(sql); ptmt.setInt(1, id); ptmt.execute(); } //查詢user public List<User> query() throws Exception{ //通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接 Connection conn = null; DataSource ds = null; Context initCtx = new InitialContext(); ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test"); if(ds!=null){ conn = ds.getConnection(); } //2.通過(guò)連接池操作數(shù)據(jù)庫(kù) Statement stmt= conn.createStatement(); //3.查詢語(yǔ)句 ResultSet rs=stmt.executeQuery("select * from account"); //4..創(chuàng)建一個(gè)將要返回的List集合對(duì)象list,,一個(gè)User的對(duì)象u List<User> list=new ArrayList<User>(); User u = null; //5.重設(shè)用戶User對(duì)象u中的屬性,,然后將重設(shè)后的對(duì)象u添加到集合對(duì)象list中 while(rs.next()){ u=new User(); u.setId(rs.getInt("id")); u.setName(rs.getString("name")); u.setPassword(rs.getString("password")); list.add(u); } return list; } public User get(Integer id) throws SQLException{ //通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接 User user=null; Connection conn = null; DataSource ds = null; PreparedStatement ptmt=null; ResultSet rs=null; Context initCtx; try { initCtx = new InitialContext(); ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test"); if(ds!=null){ conn = ds.getConnection(); } String sql=""+ " select * from account"+ " where id=? "; //2.通過(guò)連接池的連接操作數(shù)據(jù)庫(kù) ptmt=conn.prepareStatement(sql); ptmt.setInt(1,id); rs=ptmt.executeQuery(); //conn.close(); hile(rs.next()){ user=new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); } } catch (NamingException e) { e.printStackTrace(); }finally{ rs.close(); ptmt.close(); conn.close(); } return user; }
|
|