事先先在sqlyog中新建一个数据库,然后建一个表,
然后使用myeclipse新建一个Web service project,分别建立四个包,cn.edu.hpu.service,
cn.edu.hpu.servlet,
cn.edu.hpu.test,
cn.edu.hpu.util,
然后在第一个包中建立一个对象,
代码如下:
<span style="font-size:18px;">package cn.edu.hpu.model; public class user { private String name; private String password; private int code; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } } </span>
<span style="font-size:18px;"> </span>
<span style="font-size:18px;">在第二个包中:</span>
<span style="font-size:18px;"> </span>
package cn.edu.hpu.service; import cn.edu.hpu.model.user; public interface UserManager { public boolean add(user u); public boolean checkLogin(String name,String password); }
package cn.edu.hpu.service; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import cn.edu.hpu.model.user; import cn.edu.hpu.util.DBO; public class UserManagerImpl implements UserManager{ public boolean add(user u){ boolean flag = false; Connection conn=null; PreparedStatement pst=null; try{ conn=DBO.getConnection(); String sql="insert into login_ (name,password,code) value(?,?,?)"; pst=conn.prepareStatement(sql); pst.setString(1,u.getName()); pst.setString(2, u.getPassword()); pst.setInt(3, u.getCode()); int rows = pst.executeUpdate(); if(rows > 0){ flag = true; } }catch(Exception ex){ ex.printStackTrace(); }finally{ DBO.close(pst,conn); } return flag; } public boolean checkLogin(String name,String password){ boolean flag = false; Connection conn = null; Statement st = null; ResultSet rs = null; try{ conn = DBO.getConnection(); st = conn.createStatement(); String sql = "select * from login_ where name=" + name; rs = st.executeQuery(sql); while(rs.next()){ if(rs.getString("password").equals(password)){ flag=true; } } }catch(Exception e){ e.printStackTrace(); }finally{ DBO.close(rs,st,conn); } return flag; } }
package cn.edu.hpu.test; import java.sql.Connection; import java.util.Scanner; import cn.edu.hpu.model.user; import cn.edu.hpu.service.UserManager; import cn.edu.hpu.service.UserManagerImpl; import cn.edu.hpu.util.DBO; public class test { public static void main(String[] args) { Scanner input = new Scanner(System.in); Connection conn = DBO.getConnection(); if(conn != null) System.out.println("Connection success"); UserManager um = new UserManagerImpl(); for(int i = 100;i<105;i++){ user u =new user(); System.out.println("输入一个字符串:"); String ch = input.nextLine(); u.setName(ch); u.setPassword("pwd" + i); u.setCode(i + 1); boolean flag = um.add(u); } } }
package cn.edu.hpu.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBO { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/login_plus"; private static String username = "root"; private static final String password = "123"; static{ try{ Class.forName(driver); } catch(ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection(){ Connection conn = null; try{ conn = DriverManager.getConnection(url,username,password); }catch(SQLException e){ e.printStackTrace(); } return conn; } public static void close(ResultSet rs,Statement st,Connection conn){ try{ if(rs != null){ rs.close(); } if(st != null){ st.close(); } if(conn != null){ conn.close(); } }catch(SQLException e){ e.printStackTrace(); } } public static void close(Statement st, Connection conn){ close(null,st,conn); } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style> section{ background-color:#C9E495; width:360px; height:280px; text-align:center; padding-top:1px; margin-top:156px; } .textBaroder{ border-width:1px; border-style:solid; } </style> </head> <center><body> <section> <form action="addm" method="post" align="center"> <center><h1>用户信息管理系统</h1></center> <b>用户名:</b><input class="textBaroder" type="text" name="Name"><br> <p></p> <b>密 码:</b><input class="textBaroder" type="password" name="Password"><br> <font size="+1" > <p> <input class="textBaroder" name="submit" class="textBaroder" type="submit" class="button" style="width:200px; height:35px " value="登录" /> </p> <p><font size="-1" color="#000099"> 忘记密码?</font> <font size="-1" color="#000099"><a href="register.jsp">新用户</a> </font> </p> </font> </form> </section> </body></center> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style> section{ background-color:green; width:360px; height:280px; text-align:center; padding-top:1px; margin-top:156px; } .textBaroder{ border-width:1px; border-style:solid; } </style> </head> <center><section><body> <h1 align="center">注册新用户</h1> <hr/> <form action="add" method="post" align="center" > 用户名:<input class="textBaroder" name="name" type="text"/><br/> <p></p> 密 码:<input name="password" class="textBaroder" type="password"/><br/> <p></p> 验证码:<input name="code" class="textBaroder" type="text"><br/> <p></p> <input type="submit" style="width:80px; height:35px "value="注册" /> </form> <a href="index.jsp">返回 </a> </body></section></center> </html>