public interface IStudentDao {
// 保存数据
void save(Studentinfo stu);
// 修改指定学生信息
void update(Integer studentId, Studentinfo stu);
// 删除学生
void delete(int studentId);
// 获取指定学生
Studentinfo get(int studentId);
// 获取所有的学生
List<Studentinfo> getAll();
public class StudentinfoDaoImpl implements IStudentDao {
// 增
@Override
public void save(Studentinfo stu) {
String sql = "insert into studentInfo(studentName,studentAge) values(?,?)";
QueryRunner qr = new QueryRunner(JDBCUtil.getDS());
try {
qr.update(sql, stu.getStudentName(), stu.getStudentAge());
} catch (SQLException e) {
e.printStackTrace();
}
}
// 改---<有问题>
@Override
public void update(Integer studentId, Studentinfo stu) {
String sql = "update studentinfo set studentName=?,studentAge=? where studentId=?";
QueryRunner qr = new QueryRunner(JDBCUtil.getDS());
try {
qr.update(sql, stu.getStudentName(), stu.getStudentAge(), stu.getStudentId());
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删
@Override
public void delete(int studentId) {
String sql = "delete from studentinfo where studentId=?";
QueryRunner qr = new QueryRunner(JDBCUtil.getDS());
try {
qr.update(sql, studentId);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查-单一条件
@Override
public Studentinfo get(int studentId) {
String sql = "select * from studentinfo where studentId=?";
QueryRunner qr = new QueryRunner(JDBCUtil.getDS());
try {
return qr.query(sql, new BeanHandler<Studentinfo>(Studentinfo.class), studentId);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// 查-全部信息
@Override
public List<Studentinfo> getAll() {
String sql = "select * from studentinfo ";
QueryRunner qr = new QueryRunner(JDBCUtil.getDS());
try {
return qr.query(sql, new BeanListHandler<Studentinfo>(Studentinfo.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
package com.zixue.jdbc.domain;
public class Studentinfo {
private Integer studentId;
private String studentName;
private Integer studentAge;
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
// 无参公共构造器
public Studentinfo() {
super();
}
@Override
public String toString() {
return "Studentinfo [studentId=" + studentId + ", studentName=" + studentName + ", studentAge=" + studentAge
+ "]";
}
package com.zixue.jdbc.test;
import java.util.List;
import org.junit.Test;
/**
// 删 根据id删除信息
@Test
public void delete() {
IStudentDao dao = new StudentinfoDaoImpl();
dao.delete(2019315);
}
// 改 根据id修改信息
@Test
public void updata() {
Studentinfo sinfo = new Studentinfo();
sinfo.setStudentName("四大金刚");
sinfo.setStudentAge(190);
// 保存学生
IStudentDao dao = new StudentinfoDaoImpl();
dao.update(2019316, sinfo);
}
// 查 根据id查询信息
@Test
public void get() {
IStudentDao dao = new StudentinfoDaoImpl();
Studentinfo stu1 = dao.get(2019316);
System.out.println("根据id查询的学生信息:" + stu1);
}
// 查 查询全部信息
@Test
public void getAll() {
IStudentDao dao = new StudentinfoDaoImpl();
List<Studentinfo> allStudentinfos = dao.getAll();
System.out.println("查所有的学生信息:" + allStudentinfos);
}
}
package com.zixue.jdbc.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
/**
@Copyright:
/
public class JDBCUtil {
public static DataSource ds = null;// 创建连接池
/ 静态代码块保证代码执行一次(只有类被加载到JVM时才会执行一次) /
static {
try {
// 加载配置文件
Properties pp = new Properties();
// 加载类路径下
pp.load(new FileInputStream("resourece/druid.properties"));
// 创建连接池,使用配置文件中的参数
try {
ds = DruidDataSourceFactory.createDataSource(pp);/ 此处犯错,因为没导入德鲁伊jar包 */
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取数据源
public static DataSource getDS() {
return ds;
}
// 这部分也是获取数据源,可以不要了,暂时保留
public static Connection getConn() {
try {
// 建立与数据库的连接
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 关闭资源
public static void close(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.cancel();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
1:只贴重点代码即可,修改有问题。贴新增、删除干啥;
2:debug断点调试、不会也必须要学会的;
3:修改不成功有否报错,贴报错日志出来;
4:事务是否有commit动作、没有加上去。
没得报错
下次贴对应的代码
已经解决,set&get不匹配了
没得事务
已经解决!