package com.haoran.lesson2.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
//在该处写调用db.properties中关于连接目的数据库的基本信息
//在jdbcUtils类中进行配置文件的高内聚
public class jdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try{
InputStream resourceAsStream = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
//读取配置文件
Properties properties = new Properties();
properties.load(resourceAsStream);//下载资源
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
//然后开始1,加载驱动
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//新建一个getConnection的方法类来实现 获取连接Connection 的功能
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
//新建一个release的方法类来实现 释放连接资源 的功能
public static void release(Connection conn, Statement st, ResultSet rs){
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3然后进行数据库的更新和查询
更新:
package com.haoran.lesson2;
import com.haoran.lesson2.utils.jdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn=jdbcUtils.getConnection();//直接用工具类进行数据库连接
st=conn.createStatement();//利用数据库连接conn获得SQL的执行对象st
String sql="INSERT INTO `users`(id,`NAME`,`PASSWORD`,`email`,`birthday`)" +
"VALUE (4,'xiaozhang','1234','123123123@qq.com','1902-02-08')";
//每次对数据库进行更新操作,只是该语句发生变化即可;
int i = st.executeUpdate(sql);
if (i>0){
System.out.println("插入成功");
}
//当上述SQL语句为select查询语句时,st.executeQuery;
// String sql="select * from users where id = 1";
// rs =st.executeQuery(sql);
// while(rs.next()){
// System.out.println(rs.getString("NAME"));
// }
} catch (SQLException e) {
e.printStackTrace();
}finally {
jdbcUtils.release(conn,st,rs);
}
}
}
查询:
package com.haoran.lesson2;
import com.haoran.lesson2.utils.jdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class testSelect {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn= jdbcUtils.getConnection();//直接用工具类进行数据库连接
st=conn.createStatement();//利用数据库连接conn获得SQL的执行对象st
String sql="select * from users where id = 1";
rs =st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString("Name"));
}
} catch (
SQLException e) {
e.printStackTrace();
}finally {
jdbcUtils.release(conn,st,rs);
}
}
}



