CREATE TABLE `NewTable` ( `id` int(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT , `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `password` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , PRIMARY KEY (`id`) )
<form action="/iMath/servlet/AdminServlet?method=login" method="post">
<table border='1' align="center">
<caption>USER LOGIN</caption>
<tr>
<th>username</th>
<th><input type="text" name="username" /></th>
</tr>
<tr>
<th>password</th>
<th><input type="password" name="password" /></th>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
<body>
${message}
</body>
driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/imath?useUnicode=true&characterEncoding=utf-8 user = root password =1234
package cn.imath.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public final class DBUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
/**
* load the property file
*/
static{
Properties props = new Properties();
//get the class loader
InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("cn/imath/util/dbConfig.properties");
try {
props.load(is);
} catch (Exception e) {
e.printStackTrace();
}
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
}
/**
* register the driver
*/
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* OPEN THE DATABASE CONNECTION
* @return
*/
public static Connection getConn(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* CLOSE THE DATABASE CONNECTION
* @param rs
*/
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
close(rs);
close(stmt);
close(conn);
}
public static void close(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement stmt){
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package cn.imath.entity;
public class Admin {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
package cn.imath.dao;
import cn.imath.entity.Admin;
public interface AdminDao {
/**
* LOGIN METHOD
* @param username
* @param password
* @return INSTANCE OF Admin
*/
public Admin login(String username,String password);
}
package cn.imath.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.imath.dao.AdminDao;
import cn.imath.entity.Admin;
import cn.imath.util.DBUtil;
public class AdminDaoImpl implements AdminDao {
/**
* LOGIN METHOD
*/
public Admin login(String username, String password) {
String sql = "select * from admin where name=? and password=? ";
Connection conn = DBUtil.getConn();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
//set the query parameters
pstmt.setString(1,username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
int id = rs.getInt(1);
Admin ad = new Admin();
ad.setId(id);
ad.setPassword(password);
ad.setName(username);
return ad;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
package cn.imath.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.imath.dao.AdminDao;
import cn.imath.dao.impl.AdminDaoImpl;
import cn.imath.entity.Admin;
public class AdminServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
if(method!=null){
if("login".equals(method)){
this.login(request,response);
}
}
}
private void login(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
AdminDao dao = new AdminDaoImpl();
Admin ad = dao.login(username, password);
if(ad!=null){
request.setAttribute("message", "Login Success");
}else{
request.setAttribute("message", "Login Failed");
}
try {
request.getRequestDispatcher("/message.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有