Java—JBDC(經過properties配置文件連接數據庫)

public class JBDC_V1 {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
/**
 * 靜態代碼塊加載配置文件信息
 */

    static {
        try{
           ResourceBundle bundle = ResourceBundle.getBundle("db");
           driver = bundle.getString("driver");
           url = bundle.getString("url");
           username = bundle.getString("username");
           password = bundle.getString("password");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }


    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

public class JBDC_V2 {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    /**
     * 靜態代碼塊加載配置文件信息
     */
    static {
        try {
            // 1.經過當前類獲取類加載器
            ClassLoader classLoader = JBDC_V2.class.getClassLoader();
            // 2.經過類加載器的方法得到一個輸入流
            InputStream is = classLoader.getResourceAsStream("db.properties");
            // 3.建立一個properties對象
            Properties props = new Properties();
            // 4.加載輸入流
            props.load(is);
            // 5.獲取相關參數的值
            driver = props.getProperty("driver");
            url = props.getProperty("url");
            username = props.getProperty("username");
            password = props.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 獲取鏈接方法
     *
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

配置文件(放置src目錄)mysql

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8
username=root
password=

檢測類sql

public class JBDC_Test {

@Test
public void test2() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1.獲取鏈接
conn = JBDC_V2.getConnection();
// 2.編寫sql語句
String sql = "insert t1 (id,name) value (?,?)";
// 3.獲取執行sql語句對象
pstmt = conn.prepareStatement(sql);
// 4.設置參數
pstmt.setInt(1, 5);
pstmt.setString(2, "xjj");
// 5.執行刪除操做
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("刪除成功!");
} else {
System.out.println("刪除失敗!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.釋放資源
JBDC_V2.release(conn, pstmt, null);
}
}
public class JBDC_V2 {
private static String driver;
private static String url;
private static String username;
private static String password;

/**
* 靜態代碼塊加載配置文件信息
*/
static {
try {
// 1.經過當前類獲取類加載器
ClassLoader classLoader = JBDC_V2.class.getClassLoader();
// 2.經過類加載器的方法得到一個輸入流
InputStream is = classLoader.getResourceAsStream("db.properties");
// 3.建立一個properties對象
Properties props = new Properties();
// 4.加載輸入流
props.load(is);
// 5.獲取相關參數的值
driver = props.getProperty("driver");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 獲取鏈接方法
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();} } }}
相關文章
相關標籤/搜索