您的位置:首页 > 其它

工具类的第二种写法(jdbc中获取连…

2016-05-12 00:17 316 查看
package jdbc.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class JDBCUtils {

private static final String URL =
"jdbc:mysql://localhost:3306/school";

private static final String USER = "root";

private static final String PASSWORD =
"root";

public static Connection getConnection() {

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn =
DriverManager.getConnection(URL, USER, PASSWORD);

} catch (ClassNotFoundException
e) {

// TODO
Auto-generated catch block

e.printStackTrace();

} catch (SQLException e)
{

// TODO
Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static void closeResource3(Connection
conn, Statement stmt,

ResultSet rs)
{

try {

if (rs !=
null) {

rs.close();

}

} catch (SQLException e)
{

// TODO
Auto-generated catch block

e.printStackTrace();

} finally {

try {

if
(stmt != null) {

stmt.close();

}

} catch
(SQLException e) {

e.printStackTrace();

} finally
{

try
{

if
(conn != null) {

conn.close();

}

}
catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: