首页 文章 小李的java代码

小李的java代码

2021-06-03 21:26  浏览数:587  来源:小键人2088409    

package com.lgm.util;import java.io.InputStream;import java.sql.*;import java.util.Propert
ies;public class JDBCUtils { private static ThreadLocal<Connection> tl = new ThreadLoca
l(); private static Properties prop = new Properties(); static { try{
InputStream in = JDBCUtils.class.getResourceAsStream("/jdbc.properties"); p
rop.load(in); in.close(); String address = prop.getProperty("address
"); System.out.println("address = " + address); Class.forName(addres
s); }catch(Exception e){ e.printStackTrace(); } } public st
atic Connection getConnection(){ Connection coon = tl.get(); if(coon == null
){ try{ String url = prop.getProperty("url"); //S
ystem.out.println("url = " + url); String username = prop.getProperty("user
name"); String password = prop.getProperty("password"); //Sy
stem.out.println("password = " + password); coon = DriverManager.getConnect
ion(url,username,password); tl.set(coon); }catch(Exception e){
e.printStackTrace(); System.out.println(e.getMessage());
throw new RuntimeException(e); } } return coon; }
public static void close(Connection coon, PreparedStatement pr, ResultSet rs){ if
(rs != null){ try { rs.close(); } catch (SQLException
throwables) { throwables.printStackTrace(); } } i
f(pr != null){ try { pr.close(); } catch (SQLExceptio
n throwables) { throwables.printStackTrace(); } }
if(coon != null){ try { coon.close(); tl.remove()
; } catch (SQLException throwables) { throwables.printStackTrace
(); } } } public static void main(String[] args) { //System
.out.println(getConnection()); }}package com.lgm.controller;import cn.hutool.captcha.Ca
ptchaUtil;import cn.hutool.captcha.LineCaptcha;import javax.servlet.ServletException;impor
t javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import java
x.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servl
et.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOExcepti
on;@WebServlet("/validateCode")public class ValidateCodeController extends HttpServlet {
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) th
rows ServletException, IOException { LineCaptcha lc = CaptchaUtil.createLineCaptcha
(80,30); String code = lc.getCode(); HttpSession session = req.getSession();
session.setAttribute("realCode",code); ServletOutputStream out = resp.getOu
tputStream(); lc.write(out); }}package com.lgm.service.impl;import com.lgm.dao.i
mpl.FeiQDaoImpl;import com.lgm.entity.FeiQ;import com.lgm.service.FeiQService;import com.l
gm.util.JDBCUtils;import java.sql.Connection;import java.sql.SQLException;public class Fei
QServiceImpl implements FeiQService { private Connection coon = JDBCUtils.getConnection
(); private FeiQDaoImpl feiQDao = new FeiQDaoImpl(); @Override public boolean log
inFeiQ(Integer id, String password) { boolean a = false; try{ coo
n.setAutoCommit(false); //逻辑代码 FeiQ feiQ = feiQDao.selectFeiQById(id
); if(feiQ != null){ if(feiQ.getPassword().equals(password)){
a = true; }else { throw new RuntimeExce
ption("账号或密码错误"); } }else { throw new RuntimeExce
ption("用户不存在"); } coon.commit(); }catch (Exception e){
try { coon.rollback(); } catch (SQLException throwables) {
throwables.printStackTrace(); } e.printStackTrace();
throw new RuntimeException(e); }finally{ JDBCUtils.close(coon
,null,null); } return a; } @Override public Integer registerFeiQ(St
ring user_name, String paw1, String paw2) { Integer a = null; try{
coon.setAutoCommit(false); //逻辑代码 FeiQ feiQ = feiQDao.selectFeiQBy
Name(user_name); if(feiQ == null){ FeiQ feiQ1 = new FeiQ(user_na
me,paw1,null); if(paw1.equals(paw2)){ feiQDao.insertFeiQ
(feiQ1); FeiQ feiQ2 = feiQDao.selectFeiQByName(user_name);
a = feiQ2.getUser_id(); }else { throw new Runtime
Exception("两次输入的密码不一致"); } }else { throw new Runt
imeException("用户名重复"); } coon.commit(); }catch(Exception e){
try { coon.rollback(); } catch (SQLException throwabl
es) { throwables.printStackTrace(); } e.printStackTra
ce(); throw new RuntimeException(e); }finally { JDBCUtils.clo
se(coon,null,null); } return a; } @Override public boolean checkFei
Q(String user_name, Integer id) { boolean a = false; try{ coon.se
tAutoCommit(false); //逻辑代码 FeiQ feiQ = feiQDao.selectFeiQByName(user
_name); if(feiQ!=null){ if(feiQ.getUser_id().equals(id)){
a = true; }else { throw new RuntimeExceptio
n("用户名或密码错误"); } }else { throw new RuntimeExcepti
on("用户名不存在"); } coon.commit(); }catch (Exception e){
try { coon.rollback(); } catch (SQLException throwables) {
throwables.printStackTrace(); } e.printStackTrace();
throw new RuntimeException(e); }finally { JDBCUtils.close(coon,
null,null); } return a; } @Override public boolean updateFeiQ(Integ
er id,String paw1, String paw2) { boolean a = false; try{ coon.se
tAutoCommit(false); //逻辑代码 if(paw1.equals(paw2)){ a =
true; FeiQ feiQ = feiQDao.selectFeiQById(id); feiQDao.updat
eFeiQ(new FeiQ(feiQ.getUser_name(),paw1,id)); }else { throw new
RuntimeException("两次输入的密码不一致"); } coon.commit(); }catch (Exce
ption e){ try { coon.rollback(); } catch (SQLExceptio
n throwables) { throwables.printStackTrace(); } e.pri
ntStackTrace(); throw new RuntimeException(e); }finally { JDB
CUtils.close(coon,null,null); } return a; }}package com.lgm.dao.impl;impo
rt com.lgm.dao.FeiQDao;import com.lgm.entity.FeiQ;import com.lgm.util.JDBCUtils;import jav
a.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.u
til.ArrayList;import java.util.List;public class FeiQDaoImpl implements FeiQDao { @Over
ride public void insertFeiQ(FeiQ feiQ) { Connection coon; PreparedStateme
nt pr = null; try{ coon = JDBCUtils.getConnection(); String s
ql = "insert into t_feiq values(?,?,?)"; pr = coon.prepareStatement(sql);
pr.setString(1,feiQ.getUser_name()); pr.setString(2,feiQ.getPassword());
pr.setInt(3,feiQ.hashCode()); pr.executeUpdate(); }catch (Exc
eption e){ e.printStackTrace(); throw new RuntimeException(e);
}finally { JDBCUtils.close(null,pr,null); } } @Override publi
c void deleteFeiQById(Integer id) { Connection coon; PreparedStatement pr =
null; try{ coon = JDBCUtils.getConnection(); String sql = "de
lete from t_feiq where user_id = ?"; pr = coon.prepareStatement(sql);
pr.setInt(1,id); pr.executeUpdate(); }catch(Exception e){ e
.printStackTrace(); throw new RuntimeException(e); }finally {
JDBCUtils.close(null,pr,null); } } @Override public void updateFeiQ(FeiQ
feiQ) { Connection coon; PreparedStatement pr = null; try{
coon = JDBCUtils.getConnection(); String sql = "update t_feiq set user_name =
?, password = ? where user_id = ?"; pr = coon.prepareStatement(sql);
pr.setString(1,feiQ.getUser_name()); pr.setString(2,feiQ.getPassword());
pr.setInt(3,feiQ.getUser_id()); pr.executeUpdate(); }catch (Except
ion e){ e.printStackTrace(); throw new RuntimeException(e); }
finally { JDBCUtils.close(null,pr,null); } } @Override public F
eiQ selectFeiQById(Integer id) { Connection coon; PreparedStatement pr = nul
l; ResultSet rs = null; FeiQ feiQ = null; try{ coon = JDBC
Utils.getConnection(); String sql = "select * from t_feiq where user_id = ?";
pr = coon.prepareStatement(sql); pr.setInt(1,id); rs = pr.
executeQuery(); if(rs.next()){ String name = rs.getString("user_
name"); String password = rs.getString("password"); feiQ = n
ew FeiQ(name,password,id); } }catch(Exception e){ e.printStac
kTrace(); throw new RuntimeException(e); }finally { JDBCUtils
.close(null,pr,rs); } return feiQ; } @Override public List<FeiQ> se
lectFeiQ() { Connection coon; PreparedStatement pr = null; ResultSet
rs = null; List<FeiQ> feiQ = new ArrayList(); try{ String sql= "s
elect * from t_feiq"; coon = JDBCUtils.getConnection(); pr = coon.pr
epareStatement(sql); rs = pr.executeQuery(); while (rs.next()){
String name = rs.getString("user_name"); String password = rs.ge
tString("password"); Integer user_id = rs.getInt("user_id");
feiQ.add(new FeiQ(name,password,user_id)); } }catch (Exception e){
e.printStackTrace(); throw new RuntimeException(e); }finally {
JDBCUtils.close(null,pr,rs); } return feiQ; } public FeiQ sel
ectFeiQByName(String name) { Connection coon; PreparedStatement pr = null;
ResultSet rs = null; FeiQ feiQ = null; try{ coon = JDBCUtil
s.getConnection(); String sql = "select * from t_feiq where user_name = ?";
pr = coon.prepareStatement(sql); pr.setString(1,name); rs =
pr.executeQuery(); if(rs.next()){ //String name = rs.getString("
user_name"); Integer id = rs.getInt("user_id"); String passw
ord = rs.getString("password"); feiQ = new FeiQ(name,password,id);
} }catch(Exception e){ e.printStackTrace(); throw new Runt
imeException(e); }finally { JDBCUtils.close(null,pr,rs); }
return feiQ; }}package com.lgm.controller;import com.lgm.service.impl.FeiQServiceImpl;
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import ja
vax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.ser
vlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOExcep
tion;@WebServlet("/feiQ/login")public class LoginFeiQController extends HttpServlet { @
Override protected void service(HttpServletRequest req, HttpServletResponse resp) throw
s ServletException, IOException { req.setCharacterEncoding("utf-8"); //收参
String zh = req.getParameter("id"); System.out.println("zh = " + zh); S
tring password = req.getParameter("password"); System.out.println("password = " + p
assword); String va = req.getParameter("va"); System.out.println("va = " + v
a); FeiQServiceImpl feiQService = new FeiQServiceImpl(); //根据结果跳转界面 H
ttpSession session = req.getSession(); String re = (String)session.getAttribute("re
alCode"); if(re!=null&&!va.equals(re)){ resp.sendRedirect("/untitled/fei
Q/affirm"); return; } try{ //用户成功登陆 Integer
id = Integer.valueOf(zh); boolean b = feiQService.loginFeiQ(id, password);
System.out.println("b = " + b); session = req.getSession();
session.setAttribute("login",true); resp.sendRedirect("/untitled/feiQ/affirm");
//resp.sendRedirect("/untitled/lgm.html"); //req.getRequestDispatch
er("").forward(req,resp); }catch (Exception e){ //出现异常用户反回登录界面
resp.sendRedirect("/untitled/html/loginFeiQ.html"); e.printStackTrace();
} }}



声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)