今日份Java
package util; import java.sql.*; public class DBUtil { static String url = "jdbc:mysql://localhost:3306/class?useunicuee& characterEncoding=utf8";
static String username = "root";
static String password = "root";
static Connection conn = null;
static ResultSet rs = null;
static PreparedStatement ps = null; public static void init() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,username,password);
}catch(Exception e) {
System.out.println("init [SQL驱动程序初始化失败!]");
e.printStackTrace();
}
} public static int addUpdDel(String sql) {
int i = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
i = ps.executeUpdate();
}catch(SQLException e) {
System.out.println("sql数据库增删改异常!");
e.printStackTrace();
}
return i;
} public static ResultSet selectSql(String sql) {
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
}catch(SQLException e) {
System.out.println("sql数据库查询异常!");
e.printStackTrace();
}
return rs;
} public static void closeConn() {
try {
conn.close();
}catch(SQLException e) {
System.out.println("sql数据库关闭异常!");
e.printStackTrace();
} } }
package dao; import java.util.List;
import entity.User; public interface UserDao { public boolean login(String name,String pwd);//登录
public boolean register(User user);//注册
public List<User> getUserAll();//返回用户信息集合
public boolean delete(int id);//根据id删除用户
public boolean update(int id,String name,String pwd,String sex,String home,String info);//更新用户信息 }
package dao; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import entity.User;
import util.DBUtil; public class UserDaoImpl implements UserDao{
public boolean register(User user) {
boolean flag = false;
DBUtil.init();
int i = DBUtil.addUpdDel("insert into user(name,pwd,sex,home,info)" +
"values('"+user.getName()+"','"+user.getPwd()+"','"+user.getSex()+"','"+user.getHome()+"','"+user.getInfo()+"')");
if(i>0) {
flag = true;
}
DBUtil.closeConn();
return flag;
} public boolean login(String name,String pwd) {
boolean flag = false;
try {
DBUtil.init();
ResultSet rs = DBUtil.selectSql("select * from user where name ='"+name+"' and pwd='"+pwd+"'");
while(rs.next()) {
if(rs.getString("name").equals(name)&&rs.getString("pwd").equals(pwd)) {
flag = true;
}
}
DBUtil.closeConn();
}catch(SQLException e) {
e.printStackTrace();
}
return flag;
} public List<User> getUserAll(){
List<User> list = new ArrayList<User>();
try {
DBUtil.init();
ResultSet rs = DBUtil.selectSql("select * from user");
while(rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPwd(rs.getString("pwd"));
user.setSex(rs.getString("sex"));
user.setHome(rs.getString("home"));
user.setInfo(rs.getString("info"));
list.add(user);
}
DBUtil.closeConn();
return list;
}catch(SQLException e) {
e.printStackTrace();
}
return null;
} public boolean update(int id,String name,String pwd,String sex,String home,String info) {
boolean flag = false;
DBUtil.init();
String sql = "update user set name ='"+name
+"' , pwd ='"+pwd
+"' , sex ='"+sex
+"' , home ='"+home
+"' , info ='"+info+"' where id = "+id;
int i =DBUtil.addUpdDel(sql);
if(i>0) {
flag = true;
}
DBUtil.closeConn();
return flag;
} public boolean delete(int id) {
boolean flag = false;
DBUtil.init();
String sql = "delete from user where id="+id;
int i = DBUtil.addUpdDel(sql);
if(i>0) {
flag = true;
}
DBUtil.closeConn();
return flag;
} }
package entity; public class User { private int id;
private String name;
private String pwd;
private String sex;
private String home;
private String info; 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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd=pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex=sex;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home=home;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info=info;
} }
package 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 dao.UserDao;
import dao.UserDaoImpl; public class DeleteServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
int userId = Integer.parseInt(id); UserDao ud = new UserDaoImpl(); if(ud.delete(userId)) {
request.setAttribute("xiaoxi", "删除成功");
request.getRequestDispatcher("/Searchall").forward(request, response);
}else {
response.sendRedirect("index.jsp");
}
} }
package servlet; import java.io.IOException;
//import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import dao.UserDao;
import dao.UserDaoImpl; public class DengLuServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
UserDao ud = new UserDaoImpl(); if(ud.login(name, pwd)) {
request.setAttribute("xiaoxi", "欢迎用户"+name);
request.getRequestDispatcher("/success.jsp").forward(request,response);
}else {
response.sendRedirect("index.jsp");
}
} }
package servlet; import java.util.List;
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 dao.UserDao;
import dao.UserDaoImpl;
import entity.User; public class SearchallServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserDao ud = new UserDaoImpl();
List<User> userAll = ud.getUserAll();
request.setAttribute("userAll", userAll);
request.getRequestDispatcher("/showall.jsp").forward(request, response);
}
}
package servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import dao.UserDao;
import dao.UserDaoImpl;
import entity.User; public class ZhuCeServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");
String home = request.getParameter("home");
String info = request.getParameter("info"); User user = new User();
user.setName(name);
user.setPwd(pwd);
user.setSex(sex);
user.setHome(home);
user.setInfo(info); UserDao ud = new UserDaoImpl(); if(ud.register(user)) {
request.setAttribute("username", name);
request.getRequestDispatcher("/denglu.jsp").forward(request, response);
}else {
response.sendRedirect("index.jsp");
}
} }
今日份Java的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- java日期操作大全
摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个: 取得指定月份的第一天与取得指定月份的最后一天 http ...
- java实现获取当前年月日 小时 分钟 秒 毫秒
java代码实现如下 view source print? /** * 英文简写(默认)如:2010-12-01 */ public static String F ...
- Java基础——JDBC
今天学习的内容是:JDBC 通常jdbc连接分6步: 1)注册驱动: 2)建立连接: 3)创建Statement: 4)执行sql 语句: 5)处理结果集(若sql 语句为查询语句): 6)关闭连接. ...
- Java时间日期格式转换Date转String和String转Date
Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...
- java实现获取当前年、月、日 、小时 、分钟、 秒、 毫秒
转载 : https://blog.csdn.net/qq_36652619/article/details/85621020 package com.app.test; import java.te ...
- java 和js 时间 格式化(yyyy-MM-dd HH:mm:ss) 以及获取当前时间
1.js var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970 ...
- java网页日期选择框对应的星期有误
· 我用java写了个星期查询代码,有的日期对有的日期错,求帮忙看看哪里有问题!! import java.util.*;public class Test1 {public static void ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
随机推荐
- mysql 5.7中 count(0) count(*) count(主键) count(非空字段)效率比较
mysql count(0) count(*) count(主键) count(非空字段) 效率比较 写代码的时候经理在背后说了一句count(0)的效率高于count(*) ,索性全部测试了一下 结 ...
- Codeforces_821
A.直接判断每一个数. #include<bits/stdc++.h> using namespace std; ][]; int main() { ios::sync_with_stdi ...
- HDU_1455_dfs
http://acm.hdu.edu.cn/showproblem.php?pid=1455 int dfs(int all,int sum,int now),all代表剩余总长,sum,代表每段长, ...
- JAVA全栈工程师学习线路(建议收藏)
互联网技术,更新迭代迅速,用日新月异来说也不为过,所以,面对这这种大环境,对于码农尤其是那些对于初入职场的新手来说,该如何自我学习升级,往方向发展,这一点是大家都经常困惑的. 大部分人,刚开始学习的J ...
- 域名解析服务-DNS
一.DNS概述 DNS(Domain Name System)即域名系统.它使用层次结构的命名系统.将域名和IP相互映射 在整个互联网环境中连接了数以亿计的服务器以及个人主机.其中大部分网站都使用了域 ...
- centos下升级php到5.6
今天正好用空把php环境升级到5.6版本,首先我之前的环境是源码包编译的lnmp环境 首先到php官方网站上下载一个php5.6的tar包,放到机器上面后,开始安装,安装前先将nginx,mysql, ...
- [redis读书笔记] 第一部分 数据结构与对象 压缩列表
压缩列表是为了节省内存而设计的,是列表键和哈希键的底层实现之一. 压缩列表的逻辑如下,
- PPT导出图片质量太差?简单操作直接导出印刷质地图片
PPT导出图片质量太差?简单操作直接导出印刷质地图片 PPT不仅可以用于展示文档,还可以用于简单图片合成处理,同时,PPT文档还可以全部导出为图片. 默认情况下,PPT导出的图片为96DPI ...
- LVS服务原理以及搭建
一.LVS简介 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统,目的在于使用集群技术和Linux操作系统实现一个高性能.高可用的服务器.它 ...
- docker启动nginx的ssl配置
前提条件 一台云服务器(阿里云.腾讯云等的centOS) 服务器上面要有docker(安装方法这里不做介绍) 一个域名 ssl证书(两个文件:一个key后缀,一个pem后缀:生成方法很多这里不再介绍) ...