eclipse和mysql登录注册_使用Eclipse+MyEclipse+MySql开发一个用户注册登录系统

1.开启Eclipse,单击Flie→New→Project..出现New Project对话框如下图。

3526258.html

选择MyEclipse→J2EE Projects→Web Project单击Next如下图。

3526258.html

在Project Name文本框中写入logindemo其它保持默认选项单击Finish按钮。此时在Package Explorer(包浏览器)中出现我们新建的项目,把项目展开在src文件夹上单击右键选择New→Package出现New Java Package对话框如下图

3526258.html

在Name文本框中写入register单击Finish按钮。在src文件夹中新建了一个register包,用同样的方法在src文件夹中建一个login包。右键单击register包选择New→Class出现New Java Class对话框如下图

3526258.html

在Name文本框中填写Register单击Finish按钮。在register包下生成Register.java文件,更改其内容如下:

None.gifpackageregister;

None.gifimportjava.io.*;

None.gifimportjava.util.*;

None.gifimportjavax.servlet.*;

None.gifimportjavax.servlet.http.*;

None.gifimportjava.sql.*;

None.gif//import com.mysql.jdbc.*;None.gifNone.gifpublicclassRegisterextendsHttpServlet

ExpandedBlockStart.gif

ContractedBlock.gif...{  

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString name;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString pass;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString confirm;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpublicsynchronizedvoiddoPost(HttpServletRequest request,HttpServletResponse response) 

6a9c071a08f1dae2d3e1c512000eef41.pngthrowsServletException,IOException

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{ 

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngthis.name=request.getParameter("account");

6a9c071a08f1dae2d3e1c512000eef41.pngthis.pass=request.getParameter("password");

6a9c071a08f1dae2d3e1c512000eef41.pngthis.confirm=request.getParameter("confirm");

6a9c071a08f1dae2d3e1c512000eef41.png       PrintWriter out=response.getWriter();

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png//驱动程序名6a9c071a08f1dae2d3e1c512000eef41.pngString driverName="com.mysql.jdbc.Driver";

6a9c071a08f1dae2d3e1c512000eef41.png//数据库用户名6a9c071a08f1dae2d3e1c512000eef41.pngString userName="root";

6a9c071a08f1dae2d3e1c512000eef41.png//密码6a9c071a08f1dae2d3e1c512000eef41.pngString userPasswd="123";

6a9c071a08f1dae2d3e1c512000eef41.png//数据库名6a9c071a08f1dae2d3e1c512000eef41.pngString dbName="database";

6a9c071a08f1dae2d3e1c512000eef41.png//表名6a9c071a08f1dae2d3e1c512000eef41.pngString tableName="users";

6a9c071a08f1dae2d3e1c512000eef41.png//联结字符串6a9c071a08f1dae2d3e1c512000eef41.pngString url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;

6a9c071a08f1dae2d3e1c512000eef41.pngtryExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.png       Class.forName("org.gjt.mm.mysql.Driver");

6a9c071a08f1dae2d3e1c512000eef41.png       Connection connection=DriverManager.getConnection(url);

6a9c071a08f1dae2d3e1c512000eef41.png       Statement statement=connection.createStatement();

6a9c071a08f1dae2d3e1c512000eef41.png       

6a9c071a08f1dae2d3e1c512000eef41.png       String sql="SELECT * FROM"+tableName;

6a9c071a08f1dae2d3e1c512000eef41.png       ResultSet rs=statement.executeQuery(sql);

6a9c071a08f1dae2d3e1c512000eef41.png       

6a9c071a08f1dae2d3e1c512000eef41.pngwhile(true) 

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.pngif(rs.next())

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.pngif(this.name.equals(rs.getString(1)))

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{              

6a9c071a08f1dae2d3e1c512000eef41.png                   out.print("

"+"User Name is in Database!");               

6a9c071a08f1dae2d3e1c512000eef41.pngbreak;

ExpandedSubBlockEnd.gif                 }ExpandedSubBlockEnd.gif           }6a9c071a08f1dae2d3e1c512000eef41.pngelseExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.png                 sql="INSERT INTO"+tableName+"VALUES('"+this.name+"','"+this.pass+"')";

6a9c071a08f1dae2d3e1c512000eef41.png                 statement.execute(sql);

6a9c071a08f1dae2d3e1c512000eef41.png                 out.print("

"+"Register Successful!");

6a9c071a08f1dae2d3e1c512000eef41.pngbreak;

ExpandedSubBlockEnd.gif           }ExpandedSubBlockEnd.gif       }6a9c071a08f1dae2d3e1c512000eef41.pngreturn;

ExpandedSubBlockEnd.gif       }6a9c071a08f1dae2d3e1c512000eef41.pngcatch(SQLException e)

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

ExpandedSubBlockEnd.gif       }6a9c071a08f1dae2d3e1c512000eef41.pngcatch(ClassNotFoundException e)

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

ExpandedSubBlockEnd.gif       }6a9c071a08f1dae2d3e1c512000eef41.png              

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png 

6a9c071a08f1dae2d3e1c512000eef41.pngpublicsynchronizedvoiddoGet(HttpServletRequest request,HttpServletResponse response) 

6a9c071a08f1dae2d3e1c512000eef41.pngthrowsServletException,IOException

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{ 

6a9c071a08f1dae2d3e1c512000eef41.png      doPost(request,response);

ExpandedSubBlockEnd.gif    }ExpandedBlockEnd.gif}

用同样的方法在login包下建一个Login.java文件,其内容如下:

None.gifpackagelogin;

None.gifimportjava.io.*;

None.gifimportjava.util.*;

None.gifimportjavax.servlet.*;

None.gifimportjavax.servlet.http.*;

None.gifimportjava.sql.*;

None.gif//import com.mysql.jdbc.*;None.gifNone.gifpublicclassLoginextendsHttpServlet

ExpandedBlockStart.gif

ContractedBlock.gif...{  

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString name;

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString pass;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpublicsynchronizedvoiddoPost(HttpServletRequest request,HttpServletResponse response) 

6a9c071a08f1dae2d3e1c512000eef41.pngthrowsServletException,IOException

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{ 

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngthis.name=request.getParameter("account");

6a9c071a08f1dae2d3e1c512000eef41.pngthis.pass=request.getParameter("password");

6a9c071a08f1dae2d3e1c512000eef41.png       PrintWriter out=response.getWriter();

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png//驱动程序名6a9c071a08f1dae2d3e1c512000eef41.pngString driverName="com.mysql.jdbc.Driver";

6a9c071a08f1dae2d3e1c512000eef41.png//数据库用户名6a9c071a08f1dae2d3e1c512000eef41.pngString userName="root";

6a9c071a08f1dae2d3e1c512000eef41.png//密码6a9c071a08f1dae2d3e1c512000eef41.pngString userPasswd="123";

6a9c071a08f1dae2d3e1c512000eef41.png//数据库名6a9c071a08f1dae2d3e1c512000eef41.pngString dbName="database";

6a9c071a08f1dae2d3e1c512000eef41.png//表名6a9c071a08f1dae2d3e1c512000eef41.pngString tableName="users";

6a9c071a08f1dae2d3e1c512000eef41.png//联结字符串6a9c071a08f1dae2d3e1c512000eef41.pngString url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;

6a9c071a08f1dae2d3e1c512000eef41.pngtryExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.png       Class.forName("org.gjt.mm.mysql.Driver");

6a9c071a08f1dae2d3e1c512000eef41.png       Connection connection=DriverManager.getConnection(url);

6a9c071a08f1dae2d3e1c512000eef41.png       Statement statement=connection.createStatement();

6a9c071a08f1dae2d3e1c512000eef41.png       String sql="SELECT * FROM"+tableName;

6a9c071a08f1dae2d3e1c512000eef41.png       ResultSet rs=statement.executeQuery(sql); 

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngwhile(true) 

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.pngif(rs.next())

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.pngif((this.name.equals(rs.getString(1)))&&(this.pass.equals(rs.getString(2))))

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.png                  

6a9c071a08f1dae2d3e1c512000eef41.png                      out.print("

"+"Login Success!");

6a9c071a08f1dae2d3e1c512000eef41.png                  

6a9c071a08f1dae2d3e1c512000eef41.pngbreak;

ExpandedSubBlockEnd.gif                    }ExpandedSubBlockEnd.gif              }6a9c071a08f1dae2d3e1c512000eef41.pngelseExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.png                 out.print("

"+"Account or Password Is Invalid!");

6a9c071a08f1dae2d3e1c512000eef41.pngbreak;

ExpandedSubBlockEnd.gif              }ExpandedSubBlockEnd.gif          }6a9c071a08f1dae2d3e1c512000eef41.pngreturn;

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.pngcatch(SQLException e)

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

ExpandedSubBlockEnd.gif       }6a9c071a08f1dae2d3e1c512000eef41.pngcatch(ClassNotFoundException e)

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

ExpandedSubBlockEnd.gif       }6a9c071a08f1dae2d3e1c512000eef41.png              

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png 

6a9c071a08f1dae2d3e1c512000eef41.pngpublicsynchronizedvoiddoGet(HttpServletRequest request,HttpServletResponse response) 

6a9c071a08f1dae2d3e1c512000eef41.pngthrowsServletException,IOException

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{ 

6a9c071a08f1dae2d3e1c512000eef41.png      doPost(request,response);

ExpandedSubBlockEnd.gif    }ExpandedBlockEnd.gif}

在WebRoot文件夹上单击右键点击New→JSP出现Create a new JSP page对话框如下图

3526258.html

在File Name文本框中填写index.jsp单击Finish按钮。更改其内容如下:

ExpandedBlockStart.gif

ContractedBlock.gifNone.gifNone.gifExpandedBlockStart.gif

ContractedBlock.gif...6a9c071a08f1dae2d3e1c512000eef41.pngfunctionlogin_click()

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.pngform1.action="Login";

6a9c071a08f1dae2d3e1c512000eef41.pngform1.submit();

ExpandedSubBlockEnd.gif}6a9c071a08f1dae2d3e1c512000eef41.png    

6a9c071a08f1dae2d3e1c512000eef41.pngfunctionregister_click()

ExpandedSubBlockStart.gif

ContractedSubBlock.gif...{

6a9c071a08f1dae2d3e1c512000eef41.pngform1.action="register.jsp";

6a9c071a08f1dae2d3e1c512000eef41.pngform1.submit();

ExpandedBlockEnd.gif}None.gifNone.gifNone.gif

None.gifAccount:

None.gifNone.gif

None.gif None.gif None.gif None.gif

None.gif

None.gif None.gifPassword:

None.gifNone.gif

None.gif None.gif None.gif None.gif None.gif

None.gifNone.gifNone.gifNone.gif

None.gif None.gif None.gif None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值