Heim  >  Artikel  >  Datenbank  >  数据库链接_MySQL

数据库链接_MySQL

PHP中文网
PHP中文网Original
2016-05-27 13:46:361068Durchsuche


数据库链接_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();
}
}

}

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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn