发表于: 2018-09-17 23:22:24
1 393
今天完成的事情:
昨天台风在家,本来以为自己理清了spring和mybatis的思路,打算了来分院提交代码。结果发现看教程的时候感觉自己可以,自己写的时候一团乱麻,悲哀
JDBC,DAO
表结构:
配置数据库:
#数据库驱动
driver=com.mysql.jdbc.Driver
#连接数据库的URL
url=jdbc:mysql://localhost:3306/jdbcstudy
#用户名
user=root
#密码
password=123456
实体类:
package model;
public class Goddess {
int id;
String name;
String password;
String email;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id)
{
this.id = id;
}
@Override
public String toString() {
return "Goddess{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + '}';
}
}
工具类:
package model;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DbUtil {
public static Connection getConnection(){
String driver = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3306/jdbcstudy";
String USER = "root";
String PASSWORD = "123456";
Connection conn = null;
try{
//1.加载驱动程序
Class.forName(driver);
//2. 获得数据库连接
conn = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}
return conn;
}
}
DAO层:
//---------dao层--------------
package model;
import model.Goddess;
import model.DbUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class GoddessDao {
//增加
public void addGoddess(Goddess g) throws SQLException {
//获取连接
Connection conn = DbUtil.getConnection();
//sql
String sql = "INSERT INTO users(id, name, password, email)"
+"values("+"?,?,?,?)";
//预编译
PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行
//传参
ptmt.setInt(1, g.getId());
ptmt.setString(2, g.getName());
ptmt.setString(3, g.getPassword());
ptmt.setString(4, g.getEmail());
//执行
ptmt.execute();
}
//修改
public void updateGoddess(Goddess g) throws SQLException {
// Goddess g = new Goddess();
//获取连接
Connection conn = DbUtil.getConnection();
//sql, 每行加空格
String sql = "UPDATE users" +
" set name=?, password=?,email=?"+
" where id=?";
//预编译
PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行
//传参
ptmt.setInt(4, g.getId());
ptmt.setString(1, g.getName());
ptmt.setString(2, g.getPassword());
ptmt.setString(3, g.getEmail());
//执行
ptmt.execute();
}
//删除
public void delGoddess(int id) throws SQLException {
//获取连接
Connection conn = DbUtil.getConnection();
//sql, 每行加空格
String sql = "delete from users where id=?";
//预编译SQL,减少sql执行
PreparedStatement ptmt = conn.prepareStatement(sql);
//传参
ptmt.setInt(1, id);
//执行
ptmt.execute();
}
//查询
public List<Goddess> query() throws SQLException {
Connection conn = DbUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT name, email FROM users");
List<Goddess> gs = new ArrayList<Goddess>();
Goddess g = null;
while(rs.next()){
g = new Goddess();
g.setName(rs.getString("name"));
g.setEmail(rs.getString("email"));
gs.add(g);
}
return gs;
}
public Goddess get(int id ) throws SQLException {
Goddess g = null;
//获取连接
Connection conn = DbUtil.getConnection();
//sql, 每行加空格
String sql = "select *" +
" from users where id=?";
//预编译SQL,减少sql执行
PreparedStatement ptmt = conn.prepareStatement(sql);
//传参
ptmt.setInt(1, id);
//执行
ResultSet rs = ptmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("id")+"姓名"+rs.getString("name")+rs.getString("password")
+rs.getString("email"));
}
return g;
}
}
测试类:
import model.GoddessDao;
import model.Goddess;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class JdbcTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//查询
GoddessDao goddessDao = new GoddessDao();
goddessDao.get(4);
goddessDao.get(5);
//增加
Goddess g = new Goddess();
g.setId(5);
g.setName("xiaofang");
g.setPassword("123456");
g.setEmail("123@163.com");
goddessDao.addGoddess(g);
//删除
GoddessDao G = new GoddessDao();
G.delGoddess(5);
//修改
Goddess aaa = new Goddess();
aaa.setId(2);
aaa.setName("dedw");
aaa.setPassword("322");
aaa.setEmail("我勒个去@163.com");
System.out.println(aaa.toString());
GoddessDao mmm = new GoddessDao();
mmm.updateGoddess(aaa);
System.out.println("_______________");
System.out.println(aaa.toString());
mmm.get(3);
}
}
明天计划的是:
重新系统学习 junit 日志 单元测试 连接池
继续学习mybatis spring
遇到的问题:
心态有点崩
收获:
无
评论