首页 > IT职业 > IT生活 > jdbc之对面的女孩看过来
package jdbc.imooc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/---------------------DBUtil 包--------------------/
public class DBUtil {
private static final String URL="jdbc:mysql://127.0.0.1:3306/god?useUnicode=true&characterEncoding=utf-8";
private static final String USER="root";
private static final String PASSWORD="root";
private static Connection conn=null;
static {
try {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库的连接
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
} }
/-----------------------------------------GoddessAction 包------------------------------/
package jdbc.imooc.action;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import jdbc.imooc.dao.Goddessdao;
import jdbc.imooc.model.Goddess;
public class GoddessAction {
public void add(Goddess goddess) throws SQLException {
Goddessdao dao=new Goddessdao();
goddess.setSex(1);
goddess.setCreate_user("ADMIN");
goddess.setUpdate_user("ADMIN");
goddess.setIsdel(0);
dao.addGoddess(goddess);
}
public Goddess get(Integer id) throws SQLException {
Goddessdao dao = new Goddessdao();
return dao.get(id);
}
public void edit(Goddess goddess) throws SQLException {
Goddessdao dao = new Goddessdao();
dao.updateGoddess(goddess);
}
public void del(Integer id) throws SQLException {
Goddessdao dao = new Goddessdao();
dao.delGoddess(id);
}
public List query() throws Exception {
// TODO 自动生成的方法存根
Goddessdao dao = new Goddessdao();
return dao.query();
}
public List query(List }
/---------------------------------Goddessdao包 ------------------------------------/
package jdbc.imooc.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import jdbc.imooc.DBUtil;
import jdbc.imooc.model.Goddess;
public class Goddessdao {
ptmt.setString(1, g.getUser_name());
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, new Date(g.getBirthday().getTime()));
ptmt.setString(5, g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getUpdate_user());
ptmt.setInt(8, g.getIsdel());
ptmt.setInt(9, g.getId());
ptmt.execute();
}
ptmt.setInt(1, id);
//执行sql语句
ptmt.execute();
}
public List
List
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select id,user_name,age from imooc_goddess ");
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
ResultSet rs=ptmt.executeQuery();
Goddess g=null;
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
result.add(g);
}
return result; }
public List
List
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select * from imooc_goddess ");
sb.append(" where user_name like ? and mobile like ? and email like ?");
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
ptmt.setString(1, "%"+name+"%");
ptmt.setString(2, "%"+mobile+"%");
ptmt.setString(3, "%"+email+"%");
System.out.println(sb.toString());
ResultSet rs=ptmt.executeQuery();
Goddess g=null;
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
result.add(g);
}
return result; }
public List
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select * from imooc_goddess where 1=1 ");
if(params!=null&¶ms.size()>0){
for (int i = 0; i map=params.get(i);
sb.append(" and "+map.get("name")+" "+map.get("rela")+" "+map.get("value")+" ");
}
}
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
System.out.println(sb.toString());
ResultSet rs=ptmt.executeQuery();
Goddess g=null;
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
result.add(g);
}
return result; }
//循环详细信息
while (rs.next()){
g = new Goddess();
//获取详细信息
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
} return g;
}
}
/------------------------------------------------Goddess 包----------------------------------/
package jdbc.imooc.model;
import java.util.Date;
public class Goddess {
private Integer id;
private String user_name;
private Integer sex;
private Integer age;
private Date birthday;
private String email;
private String mobile;
private String create_user;
private String update_user;
private Date create_date;
private Date update_date;
private Integer isdel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getCreate_user() {
return create_user;
}
public void setCreate_user(String create_user) {
this.create_user = create_user;
}
public String getUpdate_user() {
return update_user;
}
public void setUpdate_user(String update_user) {
this.update_user = update_user;
}
public Date getCreate_date() {
return create_date;
}
public void setCreate_date(Date create_date) {
this.create_date = create_date;
}
public Date getUpdate_date() {
return update_date;
}
public void setUpdate_date(Date update_date) {
this.update_date = update_date;
}
public Integer getIsdel() {
return isdel;
}
public void setIsdel(Integer isdel) {
this.isdel = isdel;
}
//tostring 可以用于打印出从数据库中获取的数据
@Override
public String toString() {
return "Goddess n 编号:" + id + "n 姓名:" + user_name + "n 性别:"
}
/----------------------------------------View 包------------------------------------/
package jdbc.imooc.view;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import jdbc.imooc.action.GoddessAction;
import jdbc.imooc.model.Goddess;
public class View {
private static final String CONTEXT="欢迎来到用户禁区:n" +
"下面是用户禁区的功能列表:n" +
"[MAIN/M]:主菜单n" +
"[QUERY/Q]:查看全部用户的信息n" +
"[GET/G]:查看某位用户的详细信息n" +
"[ADD/A]:添加用户信息n" +
"[UPDATE/U]:更新用户信息n" +
"[DELETE/D]:删除用户信息n" +
"[SEARCH/S]:查询用户信息(根据姓名、手机号来查询)n" +
"[EXIT/E]:退出用户禁区n" +
"[BREAK/B]:退出当前功能,返回主菜单";
private static final String OPERATION_MAIN="MAIN";
private static final String OPERATION_QUERY="QUERY";
private static final String OPERATION_GET="GET";
private static final String OPERATION_ADD="ADD";
private static final String OPERATION_UPDATE="UPDATE";
private static final String OPERATION_DELETE="DELETE";
private static final String OPERATION_SEARCH="SEARCH";
private static final String OPERATION_EXIT="EXIT";
private static final String OPERATION_BREAK="BREAK";
public static void main(String[] args) {
System.out.println(CONTEXT);
//怎么保持程序一直运行
Scanner scan=new Scanner(System.in);
Goddess goddess=new Goddess();
GoddessAction action=new GoddessAction();
String prenious=null; //记忆标记
Integer step=1;
List }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2056/viewspace-2798392/ ,如需转载,请注明出处,否则将追究法律责任。