dao

创建时间:2015/8/23 14:39
更新时间:2015/8/23 14:40

package com.text.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


class Text{
     private int id;
     private String name;
     private String password;
     public Text(){
          super();
     }
     public String toString() {
        return "id="+id+" name="+name+" password"+password;
    }
     public Text (int id,String name,String password){
          super();
          this.id=id;
          this.name=name;
          this.password=password;
         
     }
     public int getId() {
          return id;
     }
     public void setId(int id) {
          this.id = id;
     }
     public String getName() {
          return name;
     }
     public void setName(String name) {
          this.name = name;
     }
     public String getPassword() {
          return password;
     }
     public void setPassword(String password) {
          this.password = password;
     }
    
}    
    
public class Dao {
     static {
          try {
               Class.forName("com.mysql.jdbc.Driver");
          } catch (ClassNotFoundException e) {
               System.out.println("加载驱动出错");
               e.printStackTrace();
          }
         
     }
    
     private Connection getConn(){
          try {
               return DriverManager.getConnection("jdbc:mysql://localhost:3306/sh", "root", "" );
          } catch (SQLException e) {
               System.out.println("数据库连接出错");
               e.printStackTrace();
          }
          return null;
     }
    
     private void release (ResultSet rs,Statement ps,Connection conn){
          if(rs!=null){
               try {
                    rs.close();
               } catch (SQLException e) {
                    e.printStackTrace();
               }
          }
         
          if(ps!=null){
               try {
                    ps.close();
               } catch (SQLException e) {
                    e.printStackTrace();
               }
          }
          if(conn!=null){
               try {
                    conn.close();
               } catch (SQLException e) {
                    e.printStackTrace();
               }
          }
     }
    
     public Text getUserById(int id){
          ResultSet rs=null;
          PreparedStatement ps=null;
          Connection conn=null;
          Text text=new Text();
          String sql="select * from t_text where id=?";
          try {
               conn=this.getConn();
               ps=conn.prepareStatement(sql);
               ps.setInt(1, id);
               rs=ps.executeQuery();
               if(rs.next()){
                    text=new Text(rs.getInt(id),rs.getString("name"),rs.getString("password"));
                    System.out.println(text);
                    return text;
               }
          } catch (SQLException e) {
               e.printStackTrace();
          }finally{
               this.release(rs, ps, conn);
          }
          return null;
     }
    
     public List<Text> getAllUses(){
          List<Text> list =new ArrayList<Text>();
          ResultSet rs=null;
          PreparedStatement ps=null;
          Connection conn =null;
          String sql ="select * from t_text ";
          try {
               conn=this.getConn();
               ps=conn.prepareStatement(sql);
               rs=ps.executeQuery();
               while (rs.next()){
                    Text text=new Text(rs.getInt("id"),rs.getString("name"),rs.getString("password"));
               list.add(text);
               }
               for(Text user: list){
                    System.out.println(user);
               }
          } catch (SQLException e) {
               e.printStackTrace();
          }
          return list;
     }
    
     public Text updateText(Text text){
          int rs=0;
          PreparedStatement ps=null;
          Connection conn=null;
          String sql="update t_text set name=?,password=? where id=?";
          try {
               conn=this.getConn();
               conn.setAutoCommit(false);
               ps=conn.prepareStatement(sql);
               ps.setString(1, text.getName());
               ps.setString(2, text.getPassword());
               ps.setInt(3,text.getId());
               rs=ps.executeUpdate();
              
               if(rs>0){
                    System.out.println("upok"+rs);
                    conn.commit();
                    return text;
               }
              
          } catch (SQLException e) {
               try {
                    conn.rollback();
               } catch (SQLException e1) {
                    e1.printStackTrace();
               }
               e.printStackTrace();
          }finally{
               this.release(null, ps, conn);
          }
          return null;
     }
     public boolean deleteText(int id){
          int rs=0;
          PreparedStatement ps=null;
          Connection conn=null;
          String sql="delete from t_text where id=?";
          try {
               conn=this.getConn();
               conn.setAutoCommit(false);
               ps=conn.prepareStatement(sql);
               ps.setInt(1,id);
               rs=ps.executeUpdate();
               if(rs>0){
                    System.out.println("deok"+rs);
                    conn.commit();
                    return true;
               }
          } catch (SQLException e) {
               try {
                    conn.rollback();
               } catch (SQLException e1) {
                    e1.printStackTrace();
               }
               e.printStackTrace();
          }finally{
               this.release(null, ps, conn);
          }
          return false;
     }
     public Text insertText(Text text){
          PreparedStatement ps=null;
          Connection conn=null;
          String sql="insert into t_text values(?,?,?)";
          try {
               conn=this.getConn();
               conn.setAutoCommit(false);
               ps=conn.prepareStatement(sql);
               ps.setInt(1,text.getId());
               ps.setString(2, text.getName());
               ps.setString(3, text.getPassword());
               int rs=ps.executeUpdate();
               conn.commit();
              
               if(rs>0){
                    System.out.println("inok"+rs);
                    return  new Text(text.getId(),text.getName(),text.getPassword());
               }
          } catch (SQLException e) {
               try {
                    conn.rollback();
               } catch (SQLException e1) {
                    e1.printStackTrace();
               }
               e.printStackTrace();
          }finally{
               this.release(null, ps, conn);
          }
          return null;
     }

    
     public static void main(String[] args) {
          Dao dao=new Dao();
          Text text=new Text(3,"ddd","444");
//          text.setId(2);
//          text.setName("bbb");
//          text.setPassword("222");
     //dao.insertText(text);
//          dao.getUserById(3);
//          dao.getAllUses();
          dao.deleteText(4);
          dao.updateText(text);
     }
}