数据库信息配置文件jdbc.properties内容如下:
具体实现测试类
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest {
public static void main(String[] args){
//使用资源绑定器绑定数据库信息配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String drive = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
//使用PreparedStatement
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1.注册驱动
//第一种方法
//Driver driver = new com.mysql.cj.jdbc.Driver();//父类指向子类
//DriverManager.registerDriver(driver);
//第二种方法 反射
Class.forName(drive);
//2.连接数据库
//配置数据库信息
//String url = "jdbc:mysql://localhost:3306/students";
//String user = "root";
//String password = "123456";
conn = DriverManager.getConnection(url,user,password);
//3.获取数据库连接对象
//4.执行SQL
//插入
/*String sql = "insert into information(id,name,age,class) values(?,?,?,?)";
ps= conn.prepareStatement(sql);
ps.setInt(1,1);
ps.setString(2, "张三");
ps.setInt(3, 18);
ps.setString(4, "计科");*/
//更新
/*String sql = "update information set name = ?,age = ? where id = ?";
ps= conn.prepareStatement(sql);
ps.setString(1, "赵六");
ps.setInt(2, 20);
ps.setInt(3, 1);*/
//删除
/*String sql = "delete from information where id = ?";
ps= conn.prepareStatement(sql);
ps.setInt(1, 1);*/
String sql = "select * from information where id = ?";
ps= conn.prepareStatement(sql);
ps.setInt(1, 1);
//int = executeUpdate专门执行DML中的insert,delete,update 返回值是影响数据库中的记录条数
/*int count = ps.executeUpdate();
System.out.println(count == 1 ? "成功" : "失败");*/
//ResultSet = executeQuery专门执行DQL语句的方法 返回
rs = ps.executeQuery();
//5.处理查询结果集(遍历)
while(rs.next()) { //rs.next()光标指向的行有数据就返回true
// //String id = rs.getString(1); //JDBC中的所以下标都是从1开始,而不是从0开始
// //String name = rs.getString(2); //getString():不管数据库里的数据类型是什么,都以String的形式取出
// //String age = rs.getString(3);
// //String Class = rs.getString(4);
//
String id = rs.getString("id");
String name = rs.getString("name");
String age = rs.getString("age");
String Class = rs.getString("class");
System.out.println(id+","+name+","+age+","+Class);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(ClassNotFoundException e){
e.printStackTrace();
}finally {
//6.释放资源
// 为了保证资源一定释放,在finally语句块中关闭资源
// 并且要遵循从小到大依次关闭
// 分别对其try..catch
try {
if(rs != null) {
rs.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try {
if(ps != null) {
ps.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
/*//使用Statement
Connection conn = null;
Statement sm = null;
ResultSet rs = null;
try {
//1.注册驱动
//第一种方法
//Driver driver = new com.mysql.cj.jdbc.Driver();//父类指向子类
//DriverManager.registerDriver(driver);
//第二种方法 反射
Class.forName(drive);
//2.连接数据库
//配置数据库信息
//String url = "jdbc:mysql://localhost:3306/students";
//String user = "root";
//String password = "123456";
conn = DriverManager.getConnection(url,user,password);
//3.获取数据库连接对象
sm= conn.createStatement();
//4.执行sql
//插入
//String sql = "insert into information(id,name,age,class) values(4,'王五',19,'计科')";
//删除
//String sql = "delete from information where id = 4";
//更新
//String sql = "update information set name = '赵六',age = 21 where id = 4";
//查询
String sql = "select id,name,age,class from information ";
//int = executeUpdate专门执行DML中的insert,delete,update 返回值是影响数据库中的记录条数
//int count = sm.executeUpdate(sql);
//System.out.println(count == 1 ? "成功" : "失败");
//ResultSet = executeQuery专门执行DQL语句的方法 返回
rs = sm.executeQuery(sql);
//5.处理查询结果集(遍历)
while(rs.next()) { //rs.next()光标指向的行有数据就返回true
//String id = rs.getString(1); //JDBC中的所以下标都是从1开始,而不是从0开始
//String name = rs.getString(2); //getString():不管数据库里的数据类型是什么,都以String的形式取出
//String age = rs.getString(3);
//String Class = rs.getString(4);
String id = rs.getString("id");
String name = rs.getString("name");
String age = rs.getString("age");
String Class = rs.getString("class");
System.out.println(id+","+name+","+age+","+Class);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(ClassNotFoundException e){
e.printStackTrace();
}finally {
//6.释放资源
// 为了保证资源一定释放,在finally语句块中关闭资源
// 并且要遵循从小到大依次关闭
// 分别对其try..catch
try {
if(rs != null) {
rs.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try {
if(sm != null) {
sm.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
*/
}
}