?? dao.java
字號:
package com.tsinghua.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.tsinghua.bean.User;
import com.tsinghua.bean.addUserBean;
import com.tsinghua.bean.pageBean;
import com.tsinghua.getDataBaseConn.GetConnection;
/*
* 什么叫DAO呢 就是 DATA ACCESS OBJECT 用來訪問數據層
* 的JAVA類 所有的對業務數據的操作都可以放到這里來
* 那我們此項目的業務功能很簡單 就是保存用戶注冊信息和
* 判斷用戶時候時候合法。所以可以寫兩個主要的方法
* 一個是注冊用的方法 一個是判斷登陸用的方法
*/
public class DAO {
/**
*連接數據庫
*
*/
private Connection conn;
private PreparedStatement pstat;
String sql="";
public boolean add(User user) throws SQLException
{
/*
* 將學生信息添加到數據庫里
*/
conn= GetConnection.getConnection();
boolean i = true ;
try
{
sql="insert into stu_info values(?,?,?,?,?,?,?,?,?,?,?)";
pstat = conn.prepareStatement(sql);
pstat.setString(1, user.getNam());
pstat.setString(2, user.getSex());
pstat.setString(3, user.getBir());
pstat.setString(4, user.getClass_());
pstat.setString(5, user.getQq());
pstat.setString(6, user.getEmail());
pstat.setString(7, user.getTel());
pstat.setString(8, user.getAddress());
pstat.setString(9, user.getJoin_time());
pstat.setString(10, user.getLik());
pstat.setString(11, user.getInfo());
pstat.executeUpdate();
pstat.close();
}
catch(Exception e)
{
e.printStackTrace() ;
}
return i;
}
/**
* 驗證用戶 先講存放到數據庫上的用戶信息取出來 然后再比對
*/
public boolean login(User user) throws SQLException
{
conn= GetConnection.getConnection();
boolean i = false ;
// 比對信息
sql = "select nam from admin where nam=? and pwd=?";
pstat = conn.prepareStatement(sql);
pstat.setString(1, user.getNam());
pstat.setString(2, user.getPwd());
ResultSet rs1 = pstat.executeQuery();
if (rs1.next())
{
i = true;
rs1.close();
pstat.close();
}
else
{
i = false ;
rs1.close();
pstat.close();
}
conn.close();
return i;
}
/**
* 得到數據庫表里學生信息的總條數
*/
public int getTotalRows(String sql)
{
conn= GetConnection.getConnection();
int i = 0;
try
{
pstat = conn.prepareStatement(sql);
ResultSet rs2 = pstat.executeQuery();
rs2.next();
i = rs2.getInt(1);
rs2.close();
pstat.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
return i;
}
/**
* 進行分頁
*
*/
public List getInfo(pageBean page)
{
int nowPage = page.getNowPage(); //當前頁數
int perPageViwe = page.getPerPageRows(); //每頁顯示的條數
int start = (nowPage - 1) * perPageViwe; //起始位置
int maxResults = perPageViwe; //每頁顯示的條數
List users = null; //存放所有用戶信息
conn= GetConnection.getConnection();
if (start >1)
{
sql = "select top "+maxResults+" * from stu_info where " +
"idx not in (select top "+start+" idx from stu_info) order by idx";
}
else
{
sql ="select top "+maxResults+" * from stu_info order by idx";
}
try {
pstat=conn.prepareStatement(sql);
ResultSet rs =pstat.executeQuery();
users = new ArrayList();
while (rs.next())
{
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
String bir = rs.getString(4);
String class_ = rs.getString(5);
String qq = rs.getString(6);
String email = rs.getString(7);
String tel = rs.getString(8);
String address = rs.getString(9);
String join_time = rs.getString(10);
String lik = rs.getString(11);
String info = rs.getString(12);
User user = new User(); // 封裝每條用戶信息
user.setIdx(id);
user.setNam(name);
user.setSex(sex);
user.setBir(bir);
user.setClass_(class_);
user.setQq(qq);
user.setEmail(email);
user.setTel(tel);
user.setJoin_time(join_time);
user.setLik(lik);
user.setInfo(info);
user.setAddress(address);
users.add(user);
}
rs.close();
pstat.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
return users;
}
/*
* 刪除用戶信息
*/
public void delete(int idx)
{
sql = "delete from stu_info where idx = "+idx;
conn = GetConnection.getConnection();
try
{
pstat = conn.prepareStatement(sql);
int rs = pstat.executeUpdate();
pstat.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
/**
* 得到f符合條件學生的信息
*/
public List getInfo(String sql)
{
List users = null; //存放一個用戶的所有信息
conn= GetConnection.getConnection();
try
{
pstat = conn.prepareStatement(sql);
ResultSet rs = pstat.executeQuery();
rs.next();
users = new ArrayList();
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
String bir = rs.getString(4);
String class_ = rs.getString(5);
String qq = rs.getString(6);
String email = rs.getString(7);
String tel = rs.getString(8);
String address = rs.getString(9);
String join_time = rs.getString(10);
String lik = rs.getString(11);
String info = rs.getString(12);
User user = new User(); // 封裝每條用戶信息
user.setIdx(id);
user.setNam(name);
user.setSex(sex);
user.setBir(bir);
user.setClass_(class_);
user.setQq(qq);
user.setEmail(email);
user.setTel(tel);
user.setJoin_time(join_time);
user.setLik(lik);
user.setInfo(info);
user.setAddress(address);
users.add(user);
rs.close();
pstat.close();
conn.close();
}
catch
(SQLException e)
{
e.printStackTrace();
}
return users;
}
/**
* 修改學生信息
*/
public void modi(User user)
{
sql ="update stu_info set sex=?,bir=?,class=?,qq=?,email=?,tel=?,address=?,lik=?,info=? where idx =?";
conn = GetConnection.getConnection();
try
{
pstat = conn.prepareStatement(sql);
pstat.setString(1, user.getSex());
pstat.setString(2, user.getBir());
pstat.setString(3, user.getClass_());
pstat.setString(4, user.getQq());
pstat.setString(5, user.getEmail());
pstat.setString(6, user.getTel());
pstat.setString(7, user.getAddress());
pstat.setString(8, user.getLik());
pstat.setString(9, user.getInfo());
pstat.setInt(10, user.getIdx());
pstat.executeUpdate();
pstat.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public List getInfo1(pageBean page,String sql)
{
List users = null; //存放所有用戶信息
conn= GetConnection.getConnection();
try {
pstat=conn.prepareStatement(sql);
ResultSet rs =pstat.executeQuery();
users = new ArrayList();
while (rs.next())
{
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
String bir = rs.getString(4);
String class_ = rs.getString(5);
String qq = rs.getString(6);
String email = rs.getString(7);
String tel = rs.getString(8);
String address = rs.getString(9);
String join_time = rs.getString(10);
String lik = rs.getString(11);
String info = rs.getString(12);
User user = new User(); // 封裝每條用戶信息
user.setIdx(id);
user.setNam(name);
user.setSex(sex);
user.setBir(bir);
user.setClass_(class_);
user.setQq(qq);
user.setEmail(email);
user.setTel(tel);
user.setJoin_time(join_time);
user.setLik(lik);
user.setInfo(info);
user.setAddress(address);
users.add(user);
}
rs.close();
pstat.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
return users;
}
public void addUser(addUserBean user)
{
sql ="insert into admin values (?,?,?,?,?)";
conn = GetConnection.getConnection();
try
{
pstat=conn.prepareStatement(sql);
pstat.setString(1,user.getName());
pstat.setString(2,user.getPwd());
pstat.setInt(3,user.getAge());
pstat.setString(4,user.getSex());
pstat.setString(5,user.getAddress());
pstat.executeUpdate();
pstat.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -