package jdbcutils;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
//释放资源
public class JdbcUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
//文件的读取,只需要读取一次即可拿到这些值,使用静态代码块。
static{
try {
//读取资源文件,获取值。
//1.创建Properties集合
Properties pro = new Properties();
//2.加载文件
//获取src路径下的文件的方式----》ClassLoader类加载器
ClassLoader classLoader = JdbcUtils.class.getClassLoader();
URL res = classLoader.getResource("Jdbc.properties");
String path = res.getPath();
System.out.println(path);
pro.load(new FileReader(path));
// pro.load(new FileReader("src/jdbcutils/jdbc.properties"));
//获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//4.注册驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//获取连接,返回连接对象。
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void close(ResultSet rs,Statement stmt, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
url=jdbc:mysql:///test
user=root
password=root
driver=com.mysql.jdbc.Driver
package jdbcutils;
import jdbcutils.JdbcUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JdbcEmp {
public static void main(String[] args) {
List<Emp1> all = new JdbcEmp().findAll();
System.out.println(all);
System.out.println(all.size());
}
public List<Emp1> findAll(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Emp1> list = null;
try {
//1.注册驱动
// try {
// Class.forName("com.mysql.jdbc.Driver");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// //获取数据库连接对象
// conn= DriverManager.getConnection("jdbc:mysql:///test", "root", "123456");
conn = JdbcUtils.getConnection();
//定义sql语句
String sql = "select * from class";
//获取执行sql对象
stmt = conn.createStatement();
//执行sql
rs = stmt.executeQuery(sql);
Emp1 emp = null;
list = new ArrayList<Emp1>();
while (rs.next()){
int id = rs.getInt("id");
String sname = rs.getString("sname");
String company = rs.getString("company");
double salary = rs.getDouble("salary");
int fanbu = rs.getInt("fanbu");
emp = new Emp1();
emp.setId(id);
emp.setSname(sname);
emp.setCompany(company);
emp.setSalary(salary);
emp.setFanbu(fanbu);
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
// if(rs!=null){
// try {
// rs.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// if(stmt!=null){
// try {
// stmt.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// if(conn!=null){
// try {
// conn.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
JdbcUtils.close(rs,stmt,conn);
}
return list;
}
}