《Struts开发实例》
ftp://61.155.65.187/upload/
用户:lhbup
密码:lhbup
--------------------
http://dev.csdn.net/user/kui
《如何在Struts 数据库应用程序中实现记录的删除、更新及链接 》______________________
UserEditDAO.java:
package emptyprj;
import emptyprj.UserEdit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import java.util.Collection;
import java.util.ArrayList;public class UserEditDAO { private Connection con;
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='UserEditAction.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 UserEditDAO(Connection con) {
this.con = con;
} public void create(UserEdit m_UserEdit) throws SQLException {
PreparedStatement ps = null;
//<createSQL>
String sql = "INSERT INTO dbo.users VALUES (?,?,?)";
//</createSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server请加入下行:
//con.setAutoCommit(true);
//SQL Server
ps = con.prepareStatement(sql);
//<create>
ps.setString(1,m_UserEdit.getUsername());
ps.setString(2,m_UserEdit.getPassword());
ps.setString(3,m_UserEdit.getRole());
//</create> if (ps.executeUpdate() != 1) {
throw new SQLException ("error.create.UserEdit");
}
} 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(UserEdit m_UserEdit,String keyID) {
PreparedStatement ps = null;
//<updateSQL>
String sql = "UPDATE dbo.users SET username = ?,password = ?,role = ? WHERE username = ?";
//</updateSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server请加入下行:
//con.setAutoCommit(true);
//SQL Server
ps = con.prepareStatement(sql);
//<update>
ps.setString(1,m_UserEdit.getUsername());
ps.setString(2,m_UserEdit.getPassword());
ps.setString(3,m_UserEdit.getRole());
//</update> ///////////////
//<updatekeyID>
ps.setString(4,keyID);
//</updatekeyID>
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.UserEdit");
} } 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 keyID) {
//<removeIDSQL>
String sql="DELETE FROM dbo.users WHERE ";
sql+="username";
sql+=" = ?";
//</removeIDSQL> PreparedStatement ps = null;
try { if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server请加入下行:
//con.setAutoCommit(true);
//SQL Server
ps = con.prepareStatement(sql);
//<setremovekeyIDdata>
ps.setString(1,keyID);
//</setremovekeyIDdata>
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.UserEdit");
} } 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 Collection findSQL(String sql,int ipage) {
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList(); try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.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++) {
UserEdit m_UserEdit= new UserEdit();
//<find>
m_UserEdit.setUsername(rs.getString(1));
m_UserEdit.setPassword(rs.getString(2));
m_UserEdit.setRole(rs.getString(3));
//</find> rs.next();
list.add(m_UserEdit); } return list; } catch (SQLException e) {
//e.printStackTrace();
//throw new RuntimeException("error.unexpected");
return list;
} finally {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}}
ftp://61.155.65.187/upload/
用户:lhbup
密码:lhbup
--------------------
http://dev.csdn.net/user/kui
《如何在Struts 数据库应用程序中实现记录的删除、更新及链接 》______________________
UserEditDAO.java:
package emptyprj;
import emptyprj.UserEdit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import java.util.Collection;
import java.util.ArrayList;public class UserEditDAO { private Connection con;
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='UserEditAction.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 UserEditDAO(Connection con) {
this.con = con;
} public void create(UserEdit m_UserEdit) throws SQLException {
PreparedStatement ps = null;
//<createSQL>
String sql = "INSERT INTO dbo.users VALUES (?,?,?)";
//</createSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server请加入下行:
//con.setAutoCommit(true);
//SQL Server
ps = con.prepareStatement(sql);
//<create>
ps.setString(1,m_UserEdit.getUsername());
ps.setString(2,m_UserEdit.getPassword());
ps.setString(3,m_UserEdit.getRole());
//</create> if (ps.executeUpdate() != 1) {
throw new SQLException ("error.create.UserEdit");
}
} 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(UserEdit m_UserEdit,String keyID) {
PreparedStatement ps = null;
//<updateSQL>
String sql = "UPDATE dbo.users SET username = ?,password = ?,role = ? WHERE username = ?";
//</updateSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server请加入下行:
//con.setAutoCommit(true);
//SQL Server
ps = con.prepareStatement(sql);
//<update>
ps.setString(1,m_UserEdit.getUsername());
ps.setString(2,m_UserEdit.getPassword());
ps.setString(3,m_UserEdit.getRole());
//</update> ///////////////
//<updatekeyID>
ps.setString(4,keyID);
//</updatekeyID>
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.UserEdit");
} } 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 keyID) {
//<removeIDSQL>
String sql="DELETE FROM dbo.users WHERE ";
sql+="username";
sql+=" = ?";
//</removeIDSQL> PreparedStatement ps = null;
try { if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server请加入下行:
//con.setAutoCommit(true);
//SQL Server
ps = con.prepareStatement(sql);
//<setremovekeyIDdata>
ps.setString(1,keyID);
//</setremovekeyIDdata>
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.UserEdit");
} } 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 Collection findSQL(String sql,int ipage) {
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList(); try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.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++) {
UserEdit m_UserEdit= new UserEdit();
//<find>
m_UserEdit.setUsername(rs.getString(1));
m_UserEdit.setPassword(rs.getString(2));
m_UserEdit.setRole(rs.getString(3));
//</find> rs.next();
list.add(m_UserEdit); } return list; } catch (SQLException e) {
//e.printStackTrace();
//throw new RuntimeException("error.unexpected");
return list;
} finally {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}}
解决方案 »
- 如何调用一个方法能使控制台上输入的数据保存到数据库中?
- mysql备份和还原,不执行shell命令,利用连接导出和导入sql脚本
- spring 与jgroup整合加载ehcache配置文件时报错
- 从Windows窗体跳转至网页
- Struts的MVC与Spring的Mvc有什么不同?
- 为什么还不行????????????/
- weblogic下面一个问题,请帮忙看看
- 帮个忙看下!这个异常是什么原因!
- 一个部署问题!高分求教
- 如何循环每次插入1000条数据,每条数据有35个字段,要求每个字段生成数据不能重复!
- 紧急求助!tomcat4.06服务mssql2000数据库取image字段会这么慢?
- 不会java,能否感受WebLogic,WebSphere等“中间件”?
struts+DAO
一个有hibernate,一个没有