1.开启Eclipse,单击Flie→New→Web Project。
2.在Project Name文本框中写入logindemo其它保持默认选项单击Finish按钮。此时在Package
Explorer(包浏览器)中出现我们新建的项目,把项目展开在src文件夹上单击右键选择New→Package出现New Java
Package对话框。
3.在Name文本框中写入register单击Finish按钮。在src文件夹中新建了一个register包,用同样的方法在src文件夹中建一个login包。右键单击register包选择New→Class出现New
Java Class对话框。
4.在Name文本框中填写Register单击Finish按钮。在register包下生成Register.java文件,更改其内容如下:
package register;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
//import com.mysql.jdbc.*;
public class Register extends
HttpServlet{ private
String name;
private
String pass;
private
String confirm;
public synchronized void
doPost(HttpServletRequest request,HttpServletResponse
response)
throws ServletException,IOException
{
this.name=request.getParameter("account");
this.pass=request.getParameter("password");
this.confirm=request.getParameter("confirm");
PrintWriter out=response.getWriter();
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="123456";
//数据库名
String dbName="database";
//表名
String tableName="users";
//联结字符串
String
url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs=statement.executeQuery(sql);
while(true) {
if(rs.next())
{
if(this.name.equals(rs.getString(1)))
{ out.print("
"+"User Name is in
Database!");