driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
user=root
password=root
public class JDBCUtils {
private static String driver;
private static String url;
private static String user;
private static String password;
// 静态代码块
static {
try {
// 1 使用Properties处理流
// 使用load()方法加载指定的流
Properties props = new Properties();
Reader is = new FileReader("db.properties");
props.load(is);
// 2 使用getProperty(key),通过key获得需要的值,
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获得连接
*/
public static Connection getConnection() {
try {
// 1 注册驱动
Class.forName(driver);
// 2 获得连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public class Demo {
@Test
public void insert(){
try{
//1,获取连接对象
Connection conn = JDBCUtils.getConnection();
//2,指定要执行的SQL语句
String sql = "INSERT INTO zhangwu(name,money,parent) VALUES(?,?,?)";
//4,获取SQL语句的执行对象 PreparedStatement
PreparedStatement ppstat = conn.prepareStatement(sql);
//5,执行SQL语句
ppstat.setString(1, "股票收入");
ppstat.setDouble(2, 5000);
ppstat.setString(3, "收入");
int line = ppstat.executeUpdate();
//6,处理结果集
System.out.println("line=" + line);
//7,关闭连接
ppstat.close();
conn.close();
} catch(SQLException e){
throw new RuntimeException(e);
}
}
}
public void insert(){
try {
//获取一个用来执行SQL语句的对象 QueryRunner
QueryRunner qr = new QueryRunner();
String sql = "INSERT INTO zhangwu(name,money,parent) VALUES(?,?,?)";
Object[] params = {"股票收入", 5500, "收入"};
Connection conn = JDBCUtils.getConnection();
int line = qr.update(conn,sql,params);// 用来完成表数据的增加、删除、更新操作
//结果集处理
System.out.println("line = " + line);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void update(){
try {
//创建一个QueryRunner对象,用来完成SQL语句的执行
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "UPDATE zhangwu SET money = money+1000 WHERE name=?";
Object[] params = {"股票收入"};
Connection conn = JDBCUtils.getConnection();
int line = qr.update(conn, sql, params);
//结果集的处理
System.out.println("line="+line);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void delete(){
try {
//创建一个QueryRunner对象,用来完成SQL语句的执行
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "DELETE FROM zhangwu WHERE name = ?";
Object[] params = {"股票收入"};
Connection conn = JDBCUtils.getConnection();
int line = qr.update(conn, sql, params);
//结果集的处理
System.out.println("line="+line);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
ArrayHandler | 将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值 |
ArrayListHandler | 将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。 |
BeanHandler | 将结果集中第一条记录封装到一个指定的javaBean中。 |
BeanListHandler | 将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中 |
ColumnListHandler | 将结果集中指定的列的字段值,封装到一个List集合中 |
ScalarHandler | 它是用于单数据。例如select count(*) from 表操作。 |
MapHandler | 将结果集第一行封装到Map集合中,Key 列名, Value 该列数据 |
MapListHandler | 将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合 |
/*
* 账务类
*/
public class ZhangWu {
private int id;
private String name;
private double money;
private String parent;
public ZhangWu() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
@Override
public String toString() { //该方法可以省略
return "ZhangWu [id=" + id + ", name=" + name + ", money=" + money + ", parent=" + parent + "]";
}
}
public class ArrayHandlerDemo {
@Test
public void method(){
try {
//获取QueryRunner对象
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "SELECT * FROM zhangwu";
Object[] params = {};
Connection conn = JDBCUtils.getConnection();
Object[] objArray = qr.query(conn, sql, new ArrayHandler(), params);
//结果集的处理
System.out.println( Arrays.toString(objArray) );
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class ArrayListHandlerDemo {
@Test
public void method(){
try {
//获取QueryRunner对象
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "SELECT * FROM zhangwu WHERE money>?";
Object[] params = {2000};
Connection conn = JDBCUtils.getConnection();
List<Object[]> list = qr.query(conn, sql, new ArrayListHandler(), params);
//结果集的处理
for (Object[] objArray : list) {
System.out.println( Arrays.toString(objArray) );
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class BeanHandlerDemo {
@Test
public void method(){
try{
//获取QueryRunner
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "SELECT * FROM zhangwu WHERE id=?";
Object[] params = {1};
Connection conn = JDBCUtils.getConnection();
ZhangWu zw = qr.query(conn, sql, new BeanHandler<ZhangWu>(ZhangWu.class), params);
//结果集处理
System.out.println(zw);
conn.close();
} catch(SQLException e){
throw new RuntimeException(e);
}
}
}
public class BeanListHandlerDemo {
@Test
public void method(){
try{
//获取QueryRunner
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "SELECT * FROM zhangwu WHERE money>?";
Object[] params = {2000};
Connection conn = JDBCUtils.getConnection();
List<ZhangWu> list = qr.query(conn, sql, new BeanListHandler<ZhangWu>(ZhangWu.class), params);
//结果集处理
for (ZhangWu zw : list) {
System.out.println(zw);
}
conn.close();
} catch(SQLException e){
throw new RuntimeException(e);
}
}
}
public class ColumnListHandlerDemo {
@Test
public void method(){
try {
//获取QueryRunner对象
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "SELECT name FROM zhangwu WHERE money>?";
Object[] params = {2000};
Connection conn = JDBCUtils.getConnection();
List<String> list = qr.query(conn, sql, new ColumnListHandler<String>(), params);
//结果集的处理
for (String str : list) {
System.out.println(str);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class ScalarHandlerDemo {
@Test
public void method(){
try {
//获取QueryRunner对象
QueryRunner qr = new QueryRunner();
//执行SQL语句
String sql = "SELECT MAX(money) FROM zhangwu";
Object[] params = {};
Connection conn = JDBCUtils.getConnection();
Double max = qr.query(conn, sql, new ScalarHandler<Double>(), params);
//结果集的处理
System.out.println("max=" + max);
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
ArrayHandler | 将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值 |
ArrayListHandler | 将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。 |
BeanHandler | 将结果集中第一条记录封装到一个指定的javaBean中。 |
BeanListHandler | 将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中 |
ColumnListHandler | 将结果集中指定的列的字段值,封装到一个List集合中 |
ScalarHandler | 它是用于单数据。例如select count(*) from 表操作。 |
MapHandler | 将结果集第一行封装到Map集合中,Key 列名, Value 该列数据 |
MapListHandler | 将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合 |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |