建立数据库连接
//1.使用Driver连接数据库,创建Driver实现类对象
Driver driver = new Driver();
//2.准备连接数据库的基本信息
String url = "jdbc:mysql://127.0.0.1:3306/test";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "root");
//3.调用Driver的接口connect(url, info) 获取数据库连接
Connection connection = driver.connect(url, info);
简化连接不同数据库,将连接数据库信息存储到文件中
InputStream in = TestJDBC.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Driver driver = (Driver)Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
通过获取驱动连接数据库,可同时获得多个驱动
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
执行Sql语句
insert,update, delete操作
//1.获取数据库连接
Connection conn = getConnection();
//2.准备sql语句
String sql = "insert into customers(name, email, birth) Values('Wayne', 'wayne@gmail.com', '1993-03-16');";
//3.执行插入
//获取Statement对象
Statement statement = conn.createStatement();
//执行sql语句,只能是insert,update,delete
statement.executeUpdate(sql);
//4.关闭连接,在finally中关闭
statement.close();
conn.close();
select操作
通过调用 Statement 对象的 excuteQuery() 方法创建该对象
ResultSet 对象以逻辑表格的形式封装了执行数据库操作的结果集
-
ResultSet 对象维护了一个指向当前数据行的游标,初始的时候,游标在第一行之前,可以通过 ResultSet 对象的 next() 方法移动到下一行
//1.获取数据库连接 Connection conn = getConnection(); //2.获取Statement Statement statement = conn.createStatement(); //3.准备sql语句 String sql = "select * from customers"; //4.执行查询 ResultSet rs = statement.executeQuery(sql); //5.处理ResultSet while(rs.next()){ int id = rs.getInt(1); String name = rs.getString("name"); String email = rs.getString(3); Date birth = rs.getDate(4); System.out.println(id + " " + name + " " + email + " " + birth); }
PreparedStatement
提高性能
防止sql注入
-
便捷sql语句拼写
//1.创建PreparedStatement
String sql = "insert into customers(name, email, birth) Values(?,?,?);";
PreparedStatement ps = conn.prepareStatement(sql);//2.赋值 ps.setString(1, "Luke"); ps.setString(2, "Luke@gmail.com"); ps.setDate(3, new Date(new java.util.Date().getTime())); //3.执行 ps.executeUpdate();
使用ResultSetMetaData和反射,简化查询,构造方法完成对不同表查询,结果生成不同对象
public static <T> T testResultSetMetaData(Class<T> clazz, String sql, Object...objects) throws Exception{
//1.建立连接
Connection conn = getConnection();
//2.获取PreparedStatement
PreparedStatement ps = conn.prepareStatement(sql);
//3.赋值
for (int i = 0; i<objects.length; i++){
ps.setObject(i+1, objects[i]);
}
//4.执行,并使用ResultSet接受
ResultSet re = ps.executeQuery();
if(re.next()){
//5.创建clazz对象
T t = clazz.newInstance();
//6、获取ResultSetMetaData,并放入对象
ResultSetMetaData rsmd = re.getMetaData();
for(int i=0; i<rsmd.getColumnCount(); i++){
String columnLable = rsmd.getColumnLabel(i+1);
Object columnValue = re.getObject(i+1);
//为对象属性赋值
Field field = clazz.getDeclaredField(columnLable);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
} else {
return null;
}
}
存储过程
//1.建立连接
Connection conn = getConnection();
//2.获取CallableStatement
String sql = "{? = call sum_salary(?,?)}";
CallableStatement callableStatement = conn.prepareCall(sql);
//3.注册参数
callableStatement.registerOutParameter(1,Types.NUMERTIC);
callableStatement.registerOutParameter(3,Types.NUMERTIC);
//4.给参数赋值
callableStatement.setInt(2,80);
//5.执行
callableStatement.execute();
//6.获取执行结果
double sunSalary = callableStatement.getDouble(1);
long empCount = callableStatement.getLong(3);
小结
- java.sql.DriverManager用来装载驱动程序,获取数据库连接。
- java.sql.Connection完成对某一指定数据库的联接
- java.sql.Statement在一个给定的连接中作为SQL执行声明的容器,他包含了两个重要的子类型。
- Java.sql.PreparedSatement 用于执行预编译的sql声明
- Java.sql.CallableStatement用于执行数据库中存储过程的调用
java.sql.ResultSet对于给定声明取得结果的途径