properties文件
user = root
pass = root
url = jdbc:mysql://127.0.0.1:3306/demo
driver = com.mysql.cj.jdbc.Driver
1.加载驱动
2.创建连接(需要路径,用户名,密码)
3.创建会话
4.发送sql
5.处理结果
6.关闭数据库资源和会话
增改删语句调用的是executeUpdate()方法,返回值是int类型,表明的是受影响的行数
查询语句:executeQuery() 方法,返回的是一个结果集,表明的是指向查询结果的地址
public class Connjdbc {
public static void main(String[] args) {
Connection conn = null;
Statement sta = null;
try {
Class.forName(PropUtils.getvalue("driver"));
conn = DriverManager.getConnection(
PropUtils.getvalue("url"),
PropUtils.getvalue("username"),
PropUtils.getvalue("password")
);
System.out.println("连接已建立"+conn);
sta = conn.createStatement();
ResultSet res = sta.executeQuery("SELECT * FROM emp");
System.out.println("查询成功"+res);
while (res.next()) {
int empno = res.getInt("empno");
String ename = res.getString("ename");
System.out.println(empno+"----"+ename);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
try {
sta.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
public class PropUtils {
private PropUtils(){
}
public static final Properties p;
static{
p = new Properties();
try {
p.load(new FileInputStream("src/a.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getvalue(String key){
return p.getProperty(key,"");
}
}
事务
一条DML语句就是一个事务,像银行转账操作是一个人转给另一个人,需要两条DML语句执行,但是过程中如果出现异常可能会发生一个人的钱减少,另一个人的钱却没有增多的情况,为了避免这种情况的发生,我们要把减少钱和增加钱两个语句控制在一个事务中,换句话来说,取消事务的自动提交,转换为手动提交,并在捕捉到的异常中把成功的DML语句回滚,成功都成功,失败都失败
手动提交
事务回滚
只有出现异常才会让事务回滚,此时需要找出现异常会执行的代码,也就是catch语句的大括号里
private static void accounts() {
Connection conn = Conn.getConn();
Statement sta = null;
try {
//设置事务的自动提交为false
conn.setAutoCommit(false);
sta = conn.createStatement();
sta.executeUpdate("update bank set balance = balance-500 where id = 1");
sta.executeUpdate("update bank set balance = balance+500 where id = 2");
System.out.println("处理成功");
//手动提交
conn.commit();
} catch (SQLException e) {
//捕捉到异常则将之前执行过的语句回滚
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
try {
sta.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
jdbc操作两张表
一张表就是一个实体类,当查询的信息牵扯到另一张表B时,我们需要再建一个表B的实体类。
为了让两张表发生关联,除了sql语句要用条件连接外,还需要让表B成为表A的属性(类与类之间也发生关联),最后根据要查询的信息,自己决定要显示的信息有哪些
public List<Emp> findEmpandDep() {
Connection conn = Conn.getConn();
String sql = "SELECT * FROM dept AS d,emp AS e WHERE d.dptno = e.dptno;";
PreparedStatement ps = null;
List<Emp> l = new ArrayList<>(10);
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int eno = rs.getInt("eno");
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int dptno = rs.getInt("dptno");
Emp f = new Emp(eno, ename, job, mgr, hiredate, sal, comm, dptno);
//获得部门表数据并封装到对象中
String dname = rs.getString("dname");
String loc = rs.getString("loc");
Dept d = new Dept(dptno,dname,loc);
f.setDept(d);
l.add(f);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseUtil.close(ps, rs, conn);
}
return l;
}
public class Emp {
private int eno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private double sal;
private double comm;
private int dptno;
private Dept dept;
resultstamentdata 将结果集向上抽取的类,可以获得表的特征
这里要用反射不会