继续总结 Java 后台的工作1
前言
对数据库的 CRUD
操作,有一定的历史了
① JDBC
-> ② DBUtils
-> ③ JdbcTemplate
-> ④ -> Hibernate
-> ⑤ MyBatis
-> ⑥ JPA+SpringData
,....
等
用了那么多的 dao
层的框架,以亲身体会各自优缺点,寻求项目的 dao
最佳方案。
但框架会用就好,基础才是最重要
Java 后台
,从学习,到参赛、做项目,已一年有余。
最近投身于 IT行业
其他有趣的方向,如 人工智能、大数据、爬虫
等
投身后,觉得有必要先对 Java后台
做点小结
也可以供萌新参看,这次是 JDBC
代码
talk is cheap show me the code
package com.cun.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
/**
* 通用 CRUD
* @author linhongcun
*
*/
public class CRUD {
/**
* 连接
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
// ?useUnicode=true&characterEncoding=utf-8解决中文乱码
"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
return conn;
}
/**
* 断开
* @param stat
* @param conn
* @throws SQLException
*/
void close(PreparedStatement stat, Connection conn) throws SQLException {
stat.close();
conn.close();
}
/**
* 1、查
* 模拟定量查询
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void select() throws ClassNotFoundException, SQLException {
String sql = "select * from t_user limit ?,?";
Connection conn = getConnection();
PreparedStatement stat = conn.prepareStatement(sql);
// 查询从第条记录起3,连续3条记录
stat.setInt(1, 2);
stat.setInt(2, 3);
ResultSet rs = stat.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("id") + "-" + rs.getString("user_name") + "-" + rs.getString("password"));
}
// 释放资源
close(stat, conn);
}
/**
* 2、改
* 模拟重置密码
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void update() throws ClassNotFoundException, SQLException {
String sql = "update t_user set password=? where user_name=?";
Connection conn = getConnection();
PreparedStatement stat = conn.prepareStatement(sql);
stat.setString(1, "777");
stat.setString(2, "张无忌");
int rs = stat.executeUpdate();
if (rs != 0) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
// 释放资源
close(stat, conn);
}
/**
* 3、增
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void add() throws ClassNotFoundException, SQLException {
String sql = "insert into t_user(user_name,password) values(?,?)";
Connection conn = getConnection();
PreparedStatement stat = conn.prepareStatement(sql);
stat.setString(1, "张无忌");
stat.setString(2, "456");
int rs = stat.executeUpdate();
if (rs == 1) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
}
// 释放资源
close(stat, conn);
}
/**
* 4、删
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void delete() throws ClassNotFoundException, SQLException {
String sql = "delete from t_user where id=2";
Connection conn = getConnection();
PreparedStatement stat = conn.prepareStatement(sql);
int rs = stat.executeUpdate();
if (rs == 1) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
// 释放资源
close(stat, conn);
}
/**
* 5、批量插入
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void adds() throws ClassNotFoundException, SQLException {
Connection conn = getConnection();
conn.setAutoCommit(false);
String sql = "insert into t_user(user_name,password) values(?,?)";
PreparedStatement stat = conn.prepareStatement(sql);
for (int i = 1; i < 10; i++) {
stat.setString(1, "令狐冲" + i);
stat.setInt(2, i);
stat.addBatch();
}
stat.executeBatch();
conn.commit();
// 释放资源
close(stat, conn);
}
/**
* 6、调用储存过程(查)
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void pro() throws ClassNotFoundException, SQLException {
Connection conn = getConnection();
String sql = "CALL `prc_test3`;";
PreparedStatement stat = conn.prepareStatement(sql);
ResultSet rs = stat.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("id") + "-" + rs.getString("user_name") + "-" + rs.getString("password"));
}
// 释放资源
close(stat, conn);
}
}
其他
笔者不喜欢长篇议论各个知识及其用法,华而不实。
具体参见代码、注释
涉及到的存储过程(如果是使用 Oracle/sqlserver
,语法还是有区别的)
①删除
DROP PROCEDURE IF EXISTS `prc_test3`;
②创建
CREATE PROCEDURE `prc_test3`()
BEGIN
select * from t_user;
END;
③执行
CALL `prc_test3`;