日期:2014-05-17 浏览次数:20836 次
<head> <title>添加用户信息</title> </head> <body> <% request.setCharacterEncoding("gb2312"); String reqUsername=request.getParameter("username"); String reqPassword=request.getParameter("userpassword"); %> <% String url="jdbc:mysql://localhost:3306/javaweb"; String user="root"; String password="admin"; Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url,user,password); }catch(ClassNotFoundException e){ out.println("找不到驱动类"); }catch (SQLException e){ out.println("连接数据库失败"); } try{//添加注册用户 String addUser="INSERT INTO user(userid,username,password)VALUES(null,?,?)"; pstmt=conn.prepareStatement(addUser); pstmt.setString(1,reqUsername); pstmt.setString(2,reqPassword); pstmt.executeUpdate(); }catch(SQLException e){ out.println("添加用户信息失败"); e.printStackTrace(); } try{ //显示所有用户, //我猜想既然每条记录后面都要有个删除按钮,应该是要加在这个语句块里的吧 String queryALL="SELECT *FROM user;"; pstmt=conn.prepareStatement(queryALL); rs=pstmt.executeQuery(); while(rs.next()) { int userid=rs.getInt(1); String username=rs.getString(2); String userpassword=rs.getString(3); out.println("用户id:"+userid+","); out.println("用户名:"+username+","); out.println("用户密码:" + userpassword +"<br>");//按钮添加在这里?如何添加? } }catch (SQLException e) { out.println("查询所有用户失败"); } try{//关闭数据库 if(pstmt!=null){ pstmt.close(); pstmt=null; } if(conn!=null){ conn.close(); conn=null; } }catch(Exception e){ out.println("数据库关闭异常"); } %> </body> </html>