📄 personinfodao.java
字号:
package pagination;
import java.sql.*;
import java.util.*;
public class PersoninfoDAO
{
private Connection conn;
private int rowCount;
private int pageCount;
private int length;
private String pagestr;
public int getLength()
{
return(this.length);
}
public void setLength(int length)
{
this.length = length;
}
public String getPagestr(int ipage)
{
String strPage = "";
if(getLength() > 0)
{
strPage += "共";
strPage += String.valueOf(rowCount);
strPage += "条记录,共";
strPage += String.valueOf(pageCount);
strPage += "页,当前是第";
strPage += String.valueOf(ipage);
strPage += "页, ";
int istart,iend;
istart = ipage - 5;
if(istart < 0)
{
istart = 0;
}
iend = istart + 10;
if(iend > pageCount)
{
iend = pageCount;
}
istart = iend - 10;
if(istart < 0)
{
istart = 0;
}
for(int i = istart;i < iend;i++)
{
strPage += "<a href='PersoninfoAction.do?action=find&search=search&page=";
strPage += String.valueOf(i + 1);
strPage += "'>";
strPage += String.valueOf(i + 1);
strPage += "</a>";
strPage += " ";
}
}
this.pagestr = strPage;
return strPage;
}
public PersoninfoDAO(Connection conn)
{
this.conn = conn;
}
public void create(Personinfo personInfo) throws SQLException
{
PreparedStatement ps = null;
String sql = "insert into Personinfo values(?,?,?,?,?,?,?)";
try
{
if(conn.isClosed())
{
throw new IllegalStateException("conn.isClosed()");
}
ps = conn.prepareStatement(sql);
//自己随便弄一个ID:)
System.out.println(Math.round(1000000000 * Math.random()));
ps.setString(1,"" + Math.round(1000000000 * Math.random()));
ps.setString(2,personInfo.getName());
ps.setByte(3,personInfo.getSex());
ps.setString(4,personInfo.getMobile());
ps.setString(5,personInfo.getAddress());
ps.setString(6,personInfo.getMemo());
ps.setDate(7,personInfo.getSysdate());
if(ps.executeUpdate() != 1)
{
throw new SQLException("error.create.PersonInfo");
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public void update(Personinfo personInfo,String id)
{
PreparedStatement ps = null;
String sql = "update Personinfo set name=?,sex=?,mobile=?,address=?,memo=? where id=?";
try
{
if(conn.isClosed())
{
throw new IllegalStateException("conn.isClosed()");
}
ps = conn.prepareStatement(sql);
ps.setString(1,personInfo.getName());
ps.setByte(2,personInfo.getSex());
ps.setString(3,personInfo.getMobile());
ps.setString(4,personInfo.getAddress());
ps.setString(5,personInfo.getMemo());
ps.setString(6,id);
if(ps.executeUpdate() != 1)
{
throw new SQLException("error.removed.PersonInfo");
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public void remove(String sql)
{
PreparedStatement ps = null;
try
{
if(conn.isClosed())
{
throw new IllegalStateException("conn.isClosed()");
}
ps = conn.prepareStatement(sql);
if(ps.executeUpdate() != 1)
{
throw new SQLException("error.removed.PersonInfo");
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public void removeID(String id)
{
String sql = "delete from Personinfo where id=?";
PreparedStatement ps = null;
try
{
if(conn.isClosed())
{
throw new IllegalStateException("conn.isClosed()");
}
ps = conn.prepareStatement(sql);
ps.setString(1,id);
if(ps.executeUpdate() != 1)
{
throw new SQLException("error.removed.PersonInfo");
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public Personinfo findByPrimaryKey(String id) throws SQLException
{
PreparedStatement ps = null;
ResultSet rs = null;
Personinfo personInfo = null;
String sql = "select * from Personinfo where id=?";
try
{
if(conn.isClosed())
{
throw new IllegalStateException("conn.isClosed()");
}
ps = conn.prepareStatement(sql);
ps.setString(1,id);
rs = ps.executeQuery();
if(rs.next())
{
personInfo = new Personinfo();
personInfo.setId(rs.getString(1));
personInfo.setName(rs.getString(2));
personInfo.setSex(rs.getByte(3));
personInfo.setMobile(rs.getString(4));
personInfo.setAddress(rs.getString(5));
personInfo.setMemo(rs.getString(6));
return personInfo;
}
else
{
throw new SQLException("error.removed.PersonInfo");
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
if(rs != null)
{
rs.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public Collection findSQL(String sql,int ipage)
{
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try
{
if(conn.isClosed())
{
throw new IllegalStateException("conn.isClosed()");
}
ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
rs.absolute( -1);
rowCount = rs.getRow();
int offset = 1;
int pagesize = getLength();
if(getLength() < 1)
{
pagesize = rowCount;
pageCount = 1;
}
else
{
pageCount = rowCount / getLength() + ((rowCount % getLength()) > 0 ? 1 : 0);
offset = (ipage - 1) * getLength() + 1;
if(offset < 1)
{
offset = 1;
}
if(offset > rowCount)
{
offset = rowCount;
}
}
rs.absolute(offset);
for(int i = 0;i < pagesize && offset < rowCount + 1;i++,offset++)
{
Personinfo personInfo = new Personinfo();
personInfo.setId(rs.getString(1));
personInfo.setName(rs.getString(2));
personInfo.setSex(rs.getByte(3));
personInfo.setMobile(rs.getString(4));
personInfo.setAddress(rs.getString(5));
personInfo.setMemo(rs.getString(6));
rs.next();
list.add(personInfo);
}
return list;
}
catch(SQLException e)
{
e.printStackTrace();
return list;
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
if(rs != null)
{
rs.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -