数据库链接_MySQL
程序员文章站
2022-04-22 21:57:25
...
数据库链接_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)!