急求用javaWeb做个注册页面的方法 我写好了javabean 数据库也连接上了 注册页面左好了 但是数据库的整间改查如何写啊 不要说按平常些就行了 具体代码啊 还有servlet 如何控制啊 小弟初学 谢谢各位大侠了 啊 最后就是注册信息都写完了 然后点击提交 信息就保存到数据库了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写一个User的JavaBean封装好,然后写一个UserDAO用来对用户的增删改查,注册的时候在Serlvet中把JSP提交过来的参数获得,然后new一个User类,把信息都set到这个User类里面,然后利用UserDAO的添加用户的方法比如(public int addUser(User user)),在Servlet里面调用该addUser方法来判断是否成功插入数据,int返回值代表数据库受影响的行数,如果成功就跳转到成功页面,否则提示失败等等,思路大致就这样 public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { //获得客户端提交的姓名和密码 String name=arg0.getParameter("name"); String pwd=arg0.getParameter("pwd"); //调用Dao RegDao regdao=new RegDao(); regDao.reg(name,pwd); } 楼上的是servlert,这是你要的增删改查public class UserDAOImpl implements UserDAO { public boolean delete(String id) { String sql_del="delete from a where a.id in("+id+")"; boolean flag=false; Connection connection=DataSourceFactory.getConnection(); try { PreparedStatement ps=connection.prepareStatement(sql_del);// ps.setString(1, id); int i=ps.executeUpdate(); if(i!=0){ flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } private String sql_getAllUser="select * from a"; public ArrayList getUser() { ArrayList arrayList=new ArrayList(); Connection connection=DataSourceFactory.getConnection(); try { PreparedStatement preparedStatement=connection.prepareStatement(sql_getAllUser); ResultSet rs=preparedStatement.executeQuery(); while(rs.next()){ User user=new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setAge(rs.getInt("age")); user.setTime(rs.getString("time")); arrayList.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } return arrayList; } private String sql_sel="select * from a where id=?"; public User getUserById(int id) { Connection connection=DataSourceFactory.getConnection(); User user=new User(); try { PreparedStatement ps=connection.prepareStatement(sql_sel); ps.setInt(1, id); ResultSet rs=ps.executeQuery(); while(rs.next()){ user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setAge(rs.getInt("age")); user.setTime(rs.getString("time")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return user; } private String sql_insert="insert into a(id,name,age,time) values(?,?,?,?)"; public boolean inser(User user) { boolean flag=false; Connection connection=DataSourceFactory.getConnection(); try { PreparedStatement ps=connection.prepareStatement(sql_insert); ps.setInt(1, user.getId()); ps.setString(2, user.getName()); ps.setInt(3, user.getAge()); ps.setString(4, user.getTime()); int t=ps.executeUpdate(); if(t!=0){ flag=true; } } catch (SQLException e) { e.printStackTrace(); }finally{ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return flag; } private String sql_getId="select a_seq.nextval as id from dual"; public int getId() { Connection connection=DataSourceFactory.getConnection(); int id=0; try { PreparedStatement ps=connection.prepareStatement(sql_getId); ResultSet rs=ps.executeQuery(); while(rs.next()){ id=rs.getInt("id"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return id; } private String sql_edit="update a set a.name=?,a.age=?,a.time=? where a.id=?"; public boolean edit(User user) { boolean flag=false; Connection connection=DataSourceFactory.getConnection(); try { PreparedStatement ps=connection.prepareStatement(sql_edit); ps.setString(1, user.getName()); ps.setInt(2, user.getAge()); ps.setString(3, user.getTime()); ps.setInt(4, user.getId()); int i=ps.executeUpdate(); if(i!=0){ flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return flag; }} datatimepicker问题,请高手帮帮忙 表中的类别字段,数据库和java中各用什么数据类型最好? 配了个STRUTS的WEB.XML文档有问题 ! jsp中如何实现图形化显示 关于用urlrewrite 重写路径不对的问题 resin 怎么解析XML??? 请教一个有关"jsp连接Sql Server7.0/2000数据库"的问题!! 请帮忙查一下代码的错误! tomcat正常启动,域名、localhost、127.0.0.1均无法打开,昨天还正常呢?怎么回事? 一个算法问题,求大神分析看能解决不 关于struts1 html:multibox 的一个问题 LDAP认证
throws ServletException, IOException {
//获得客户端提交的姓名和密码
String name=arg0.getParameter("name");
String pwd=arg0.getParameter("pwd");
//调用Dao
RegDao regdao=new RegDao();
regDao.reg(name,pwd);
}
public class UserDAOImpl implements UserDAO {
public boolean delete(String id) {
String sql_del="delete from a where a.id in("+id+")";
boolean flag=false;
Connection connection=DataSourceFactory.getConnection();
try {
PreparedStatement ps=connection.prepareStatement(sql_del);
// ps.setString(1, id);
int i=ps.executeUpdate();
if(i!=0){
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
} private String sql_getAllUser="select * from a";
public ArrayList getUser() {
ArrayList arrayList=new ArrayList();
Connection connection=DataSourceFactory.getConnection();
try {
PreparedStatement preparedStatement=connection.prepareStatement(sql_getAllUser);
ResultSet rs=preparedStatement.executeQuery();
while(rs.next()){
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setTime(rs.getString("time"));
arrayList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return arrayList;
} private String sql_sel="select * from a where id=?";
public User getUserById(int id) {
Connection connection=DataSourceFactory.getConnection();
User user=new User();
try {
PreparedStatement ps=connection.prepareStatement(sql_sel);
ps.setInt(1, id);
ResultSet rs=ps.executeQuery();
while(rs.next()){
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setTime(rs.getString("time"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return user;
} private String sql_insert="insert into a(id,name,age,time) values(?,?,?,?)";
public boolean inser(User user) {
boolean flag=false;
Connection connection=DataSourceFactory.getConnection();
try {
PreparedStatement ps=connection.prepareStatement(sql_insert);
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setInt(3, user.getAge());
ps.setString(4, user.getTime());
int t=ps.executeUpdate();
if(t!=0){
flag=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
private String sql_getId="select a_seq.nextval as id from dual";
public int getId() {
Connection connection=DataSourceFactory.getConnection();
int id=0;
try {
PreparedStatement ps=connection.prepareStatement(sql_getId);
ResultSet rs=ps.executeQuery();
while(rs.next()){
id=rs.getInt("id");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return id;
}
private String sql_edit="update a set a.name=?,a.age=?,a.time=? where a.id=?";
public boolean edit(User user) {
boolean flag=false;
Connection connection=DataSourceFactory.getConnection();
try {
PreparedStatement ps=connection.prepareStatement(sql_edit);
ps.setString(1, user.getName());
ps.setInt(2, user.getAge());
ps.setString(3, user.getTime());
ps.setInt(4, user.getId());
int i=ps.executeUpdate();
if(i!=0){
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}}