使用myeclipse与SQLyog怎么建立简单的登陆页面
事先先在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);
}
}
然后在webroot下新建jsp文件,第一个为主页面:index.jsp
<%@ 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>
然后为简单的注册页面:register.jsp
<%@ 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>
这对一个刚接触Javaweb的小白是多么的重要啊,忽然之间感觉自己学的好充实,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理