java数据库连接(三)

    因为每次使用 JDBC 都要写一些重复的代码,我们可以直接抽取出一个工具类来使用。我们把主要目的是想通过工具类返回数据库连接,然后就可以直接通过连接来操作了。

JDBCUtilsConfig工具类

package utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtilsConfig {

private static Connection conn;
private static String driverClass;
private static String url;
private static String user;
private static String password;

static {
    
    try {
        //1.先调用加载配置文件的方法,读取配置文件
        ReadingConfig();
        
        //2.加载驱动
        Class.forName(driverClass);
        
        //3.连接数据库
        conn = DriverManager.getConnection(url, user, password);
        
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


//读取配置文件
private static void ReadingConfig() throws Exception {
    
    //1.使用类加载器读取配置文件
    InputStream inStream = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
    
    //2.获取Properties对象
    Properties pro = new Properties();
    
    //3.加载输入流
    pro.load(inStream);
    
    //4.解析对应的资源
    driverClass = pro.getProperty("driver");
    url = pro.getProperty("url");
    user = pro.getProperty("user");
    password = pro.getProperty("password");
    
}


//返回数据库连接
public static Connection Getconnection() {
    return conn;
}


//关闭连接,释放资源
public static void CloseSource(ResultSet rs,PreparedStatement ptmt,Connection conn) {
    if(rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if(ptmt != null) {
        try {
            ptmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if(conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

数据库的连接

package DatabaseConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import domain.Student;
import utils.JDBCUtilsConfig;

public class practice05 {

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    //1.读写配置文件,获取数据库连接
    Connection conn = JDBCUtilsConfig.Getconnection();
    //System.out.println(conn);
    
    //2.准备sql语句
    String sql = "select *from student";
    
    //3.预编译
    PreparedStatement ptmt = conn.prepareStatement(sql);
    
    //4.执行查询
    ResultSet rs = ptmt.executeQuery();
    
    //5.处理获取到的结果
    List liststu = new ArrayList();
    
    while(rs.next()) {
        
        Student student = new Student(rs.getString("Sno"),rs.getString("Sname"),rs.getString("Ssex"),rs.getInt("Sage"),rs.getString("Sdept"),rs.getString("S_entrance"));
        liststu.add(student);
    }
    
    
    //6.使用迭代器遍历
    Iterator iterator = liststu.iterator();
    while(iterator.hasNext()) {
        Student stu = iterator.next();
        System.out.println(stu);
    }
    
    
    //7.释放资源
    JDBCUtilsConfig.CloseSource(rs, ptmt, conn);
}

}

你可能感兴趣的:(java数据库连接(三))