Home > Database > Mysql Tutorial > 数据库链接_MySQL

数据库链接_MySQL

PHP中文网
Release: 2016-05-27 13:46:36
Original
1122 people have browsed it


数据库链接_MySQL

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

 

public class DBUtils {
private static final String URL="jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8";
private static final String UER_NAME = "root";
private static final String PDW="123123";

private static DBUtils me = new DBUtils();


public static DBUtils getInstance(){
return me;
}

//为了是单例模式成为唯一可以获取该对象的方法,将该类的无参构造函数设为私有
private DBUtils(){}

/*
* 数据库连接
*/
public Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, UER_NAME,PDW);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;

}

/*
* 释放资源
*/
public void releaseRes(Connection conn,PreparedStatement pstmt,ResultSet rs){

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

}
Copy after login

以上就是数据库链接_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template