(2)数据库的学习

//下面实现的是增删改查的模板,我尽力都解释 

  1 class DbUtil {//这个是连接数据库的工具类,是用来调用的(当连接数据库的时候)
  2 //定义变量,URL是数据库的地址,USER是登录数据库需要的账号,而PASSWORD是登录的密码
  3     public static final String URL = "jdbc:mysql://localhost:3306/imooc";
  4     public static final String USER = "liulx";
  5     public static final String PASSWORD = "123456";
  6 //conn这个是Connection类的对象
  7     private static Connection conn = null;
  8 //静态块
  9     static{
 10 //tryz--catch就是你感觉代码可能出错,然后出错后你要怎么做(catch里面)
 11 //这个是防止连接失败的时候
 12         try {
 13             //1.加载驱动程序
 14             Class.forName("com.mysql.jdbc.Driver");
 15             //2. 获得数据库连接
 16             conn = DriverManager.getConnection(URL, USER, PASSWORD);
 17         } catch (ClassNotFoundException e) {
 18             e.printStackTrace();
 19         } catch (SQLException e) {
 20             e.printStackTrace();
 21         }
 22     }
 23 
 24 //返回对象,可以在外面直接类.方法名调用,因为它被static修饰,是静态方法
 25     public static Connection getConnection(){
 26         return conn;
 27     }
 28 }
 29 
 30 //模型(实体类)与数据库里面的列相对应,我理解成是数据库的映射,就像镜子中的你
 31 package liulx.model;
 32 
 33 import java.util.Date;
 34 
 35 public class Goddess {
 36 
 37     private Integer id;
 38     private String user_name;
 39     private Integer sex;
 40     private Integer age;
 41     private Date birthday; //注意用的是java.util.Date
 42     private String email;
 43     private String mobile;
 44     private String create_user;
 45     private String update_user;
 46     private Date create_date;
 47     private Date update_date;
 48     private Integer isDel;
 49 //getter setter方法是方便外面的赋值和调用,get就是获取,set就是设置
 50     //getter setter方法。。。
 51 }
 52 
 53 //---------dao层--------------
 54 //dao层就是实现类(我理解就是增删改查的实现代码)
 55 package liulx.dao;
 56 //import是包,这个一般都是自动的
 57 import liulx.db.DbUtil;
 58 import liulx.model.Goddess;
 59 
 60 import java.sql.Connection;
 61 import java.sql.ResultSet;
 62 import java.sql.SQLException;
 63 import java.sql.Statement;
 64 import java.util.ArrayList;
 65 import java.util.List;
 66 
 67 
 68 
 69 public class GoddessDao {
 70     //增加操作的方法
 71     public void addGoddess(Goddess g) throws SQLException {
 72         //获取连接
 73         Connection conn = DbUtil.getConnection();//这个是上面工具类调用其方法,返回conn
 74         //sql
 75 //这个是对数据库的操作的sql语言
 76         String sql = "INSERT INTO imooc_goddess(user_name, sex, age, birthday, email, mobile,"+
 77             "create_user, create_date, update_user, update_date, isdel)"
 78                 +"values("+"?,?,?,?,?,?,?,CURRENT_DATE(),?,CURRENT_DATE(),?)";
 79         //预编译
 80         PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行
 81 
 82         //传参
 83         ptmt.setString(1, g.getUser_name());
 84         ptmt.setInt(2, g.getSex());
 85         ptmt.setInt(3, g.getAge());
 86         ptmt.setDate(4, new Date(g.getBirthday().getTime()));
 87         ptmt.setString(5, g.getEmail());
 88         ptmt.setString(6, g.getMobile());
 89         ptmt.setString(7, g.getCreate_user());
 90         ptmt.setString(8, g.getUpdate_user());
 91         ptmt.setInt(9, g.getIsDel());
 92 
 93         //执行
 94         ptmt.execute();
 95     }
 96 
 97 //更新操作方法
 98     public void updateGoddess(){
 99         //获取连接
100         Connection conn = DbUtil.getConnection();
101         //sql, 每行加空格,有点像增加方法
102         String sql = "UPDATE imooc_goddess" +
103                 " set user_name=?, sex=?, age=?, birthday=?, email=?, mobile=?,"+
104                 " update_user=?, update_date=CURRENT_DATE(), isdel=? "+
105                 " where id=?";
106         //预编译
107         PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行
108 
109         //传参
110         ptmt.setString(1, g.getUser_name());
111         ptmt.setInt(2, g.getSex());
112         ptmt.setInt(3, g.getAge());
113         ptmt.setDate(4, new Date(g.getBirthday().getTime()));
114         ptmt.setString(5, g.getEmail());
115         ptmt.setString(6, g.getMobile());
116         ptmt.setString(7, g.getUpdate_user());
117         ptmt.setInt(8, g.getIsDel());
118         ptmt.setInt(9, g.getId());
119 
120         //执行
121         ptmt.execute();
122     }
123 
124 
125 //删除操作方法
126 public void delGoddess(){
127         //获取连接
128 Connection conn = DbUtil.getConnection();
129         //sql, 每行加空格
130 String sql = "delete from imooc_goddess where id=?";
131         //预编译SQL,减少sql执行
132 PreparedStatement ptmt = conn.prepareStatement(sql);
133 
134         //传参
135 ptmt.setInt(1, id);
136 
137         //执行
138 ptmt.execute();
139     }
140 
141 //查询,列出sql数据
142 public List<Goddess> query() throws SQLException {
143 //获取连接
144 Connection conn = DbUtil.getConnection();
145 //
146 Statement stmt = conn.createStatement();
147         ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess");
148 
149         List<Goddess> gs = new ArrayList<Goddess>();
150         Goddess g = null;
151 //将数据列出来
152 while(rs.next()){
153             g = new Goddess();
154             g.setUser_name(rs.getString("user_name"));
155             g.setAge(rs.getInt("age"));
156 
157             gs.add(g);
158         }
159         return gs;
160     }
161 
162     public Goddess get(){
163         Goddess g = null;
164         //获取连接
165 Connection conn = DbUtil.getConnection();
166         //sql, 每行加空格
167 String sql = "select * from  imooc_goddess where id=?";
168         //预编译SQL,减少sql执行
169 PreparedStatement ptmt = conn.prepareStatement(sql);
170         //传参
171 ptmt.setInt(1, id);
172         //执行
173 ResultSet rs = ptmt.executeQuery();
174         while(rs.next()){
175             g = new Goddess();
176             g.setId(rs.getInt("id"));
177             g.setUser_name(rs.getString("user_name"));
178             g.setAge(rs.getInt("age"));
179             g.setSex(rs.getInt("sex"));
180             g.setBirthday(rs.getDate("birthday"));
181             g.setEmail(rs.getString("email"));
182             g.setMobile(rs.getString("mobile"));
183             g.setCreate_date(rs.getDate("create_date"));
184             g.setCreate_user(rs.getString("create_user"));
185             g.setUpdate_date(rs.getDate("update_date"));
186             g.setUpdate_user(rs.getString("update_user"));
187             g.setIsDel(rs.getInt("isdel"));
188         }
189         return g;
190     }
191 }
View Code

 

上面有解析,我尽量每个代码都讲

posted @ 2020-04-29 20:54  赚钱买个打印机  阅读(145)  评论(0编辑  收藏  举报