訪問Base

private static final  String  URL="jdbc:oracle:thin:@localhost:1521:HEXING";
private static final  String DRIVERNAME="oracle.jdbc.driver.OracleDriver";
private static final  String NAME="he";
private static final  String PASSWORD="he";


private   Connection  conn;




public  Connection getConn(){
try{
if(conn==null){
Class.forName(DRIVERNAME);
conn=DriverManager.getConnection(URL, NAME, PASSWORD);
    

}
return conn;
}
catch(Exception e){
return null;

  
}


}
public void closeConn(ResultSet rs, PreparedStatement ps,Connection conn){
try {
if(rs!=null){rs.close();}
if(ps!=null){ps.close();}
if(conn!=null){conn.close();}
} catch (SQLException e) {

e.printStackTrace();
}


}

-------------------------------------------------------------------------------------=======------------------------------------------------------------

 Connection conn=null;
  PreparedStatement ps=null;
     ResultSet rs=null;
    public User log(String name)  {
      
conn =getConn();
System.out.println(conn);
StringBuffer sb =new StringBuffer();
sb.append(" select * from  UserTable ");
sb.append(" where NAME = ?");
try{
ps= conn.prepareStatement(sb.toString());
ps.setString(1, name);
    rs=ps.executeQuery();
    User user = null;
  
    while(rs.next()){
    user =new User();
     System.out.println("999999999"+rs.getInt("id"));
    user.setId(rs.getInt("id"));
    user.setName(rs.getString("name"));
    user.setPassword(rs.getString("password"));
    user.setE_mail(rs.getString("E_MAIL"));
   
    }
    return user;
}catch(Exception e){
    System.out.println(e+"????");
    return null;
    }finally{
    super.closeConn(rs, ps, conn);
     
     
    }
    

    }


public List<User> getInfo() {

conn =getConn();
StringBuffer sb =new StringBuffer();
sb.append(" select *  from  UserTable ");
try{
    ps=conn.prepareStatement(sb.toString()); 
rs= ps.executeQuery();

List<User> list =new ArrayList<User>();
User us=null;
      while(rs.next()){
      us=new User();
      us.setId( rs.getInt("id"));
      us.setName(rs.getString("NAME"));
      us.setPassword(rs.getString("password"));
      us.setE_mail(rs.getString("E_MAIL"));
      list.add(us);
  }
      
return list;
}catch(Exception e){
return null;
}finally{
super.closeConn(rs, ps, conn);
 
}
}


public boolean update(User us) {
conn =getConn();
boolean judge=false;
StringBuffer sb =new StringBuffer();
sb.append(" update  UserTable set ");
if(!us.getName().isEmpty()&&us.getName()!=null&&us.getPassword().isEmpty()&&us.getPassword()==null){
System.out.println("用戶爲空");
sb.append("name = ?");

}
if(!us.getPassword().isEmpty()&&us.getPassword()!=null&&us.getName().isEmpty()&&us.getName()==null){

sb.append("password = ?");
System.out.println("密碼爲空");
}
if(!us.getPassword().isEmpty()&&us.getPassword()!=null&&!us.getPassword().isEmpty()&&us.getPassword()!=null){
sb.append(" name = ? , ");
sb.append(" password = ?");
System.out.println("2者都不爲空");
}


try {
ps=conn.prepareStatement(sb.toString());
if(!us.getName().isEmpty()&&us.getName()!=null&&us.getPassword().isEmpty()&&us.getPassword()==null){
ps.setString(1,us.getName());
System.out.println("用戶爲空");

}
if(!us.getPassword().isEmpty()&&us.getPassword()!=null&&us.getName().isEmpty()&&us.getName()==null){

ps.setString(1, us.getPassword());
System.out.println("密碼爲空");
}
if(!us.getPassword().isEmpty()&&us.getPassword()!=null&&!us.getPassword().isEmpty()&&us.getPassword()!=null){
ps.setString(1, us.getName());
ps.setString(2, us.getPassword());
System.out.println("2者都不爲空");
}
  int i=ps.executeUpdate();
if(i>0){
judge=true;

}
} catch (SQLException e) {

e.printStackTrace();
judge= false;
}finally{
super.closeConn(rs, ps, conn);
 
}
return judge;

}


public boolean delete(int id)  {
boolean judge=false;
conn =getConn();
StringBuffer sb =new StringBuffer();
sb.append("delete from  UserTable where id=?");
try {
ps=conn.prepareStatement(sb.toString());
ps.setInt(1, id);


int i = ps.executeUpdate();
if(i>0){
judge=true;

}
} catch (SQLException e) {

e.printStackTrace();

judge=false;
}
return judge;

}


public boolean insert(User us) {
boolean judge=false;
conn =getConn();
StringBuffer sb =new StringBuffer();
sb.append("insert into UserTable values(user_ids.nextval,?,?,?)");
try {
ps=conn.prepareStatement(sb.toString());
ps.setString(1, us.getName());
ps.setString(2, us.getPassword());
ps.setString(3, us.getE_mail());
int i = ps.executeUpdate();
if(i>0){
judge=true;

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

judge=false;
}
return judge;

}


}


   <meta http-equiv="refresh" content="0;url=showLogin_showUser">


<constant name="struts.i18n.encoding" value="utf-8"/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章