package com.ningmeng.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.mail.Session;
import com.ningmeng.model.User;
import com.ningmeng.util.DbUtil;
public class UserDao {
public int login(String username, String password) throws Exception {
Connection con = (Connection) DbUtil.getCon();
String sql = "select * from user where username=? and password=?";
try{
PreparedStatement ps = con.prepareStatement(sql);//
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return 1;// chenggong
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
DbUtil.getClose(con);
}
return 0;
}
public int register(String id, String username, String password)
throws Exception {
Connection con = (Connection) DbUtil.getCon();
String sql = "INSERT INTO user(id,username,password)VALUES('" + id
+ "','" + username + "','" + password + "')";
try{
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery("select username from user ");
while (rs.next()) {
if (username.equals(rs.getString("username"))) {
return 0;
}
}
ps.executeUpdate(sql);
}catch(SQLException e) {
e.printStackTrace();
}finally{
DbUtil.getClose(con);
}
return 1;
}
public ArrayList list(String username) throws Exception {
ArrayList userlist = new ArrayList();
Connection con = (Connection) DbUtil.getCon();
String sql = "select * from user where username=? ";
try{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
userlist.add(user);
//return 1;// chenggong
}}catch(SQLException e) {
e.printStackTrace();
}finally{
DbUtil.getClose(con);
}
//return 0;
return userlist;
}
/*public static void main(String[] args) throws SQLException { UserDao dao
= new UserDao(); int i; try { i = dao.list("f");
System.out.println(i); } catch (Exception e) {
e.printStackTrace(); } }*/
}