码迷,mamicode.com
首页 > 数据库 > 详细

jdbc连接Mysql方法封装

时间:2014-08-12 03:31:24      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:mysql   jdbc   

package com.shopping.util;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class DB {

static {

try {

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

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public DB(){

}

public static Connection getConn(){

Connection conn = null;

try {

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs?user=root");

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static Statement getStmt(Connection conn){

Statement stmt = null;

try {

stmt = conn.createStatement();

} catch (SQLException e) {

e.printStackTrace();

}

return stmt;

}

public static PreparedStatement getPrepStmt(Connection conn,String sql){

PreparedStatement pstmt = null;

try {

pstmt = conn.prepareStatement(sql);

} catch (SQLException e) {

e.printStackTrace();

}

return pstmt;

}

public static ResultSet getRset(Statement stmt,String sql){

ResultSet rs = null;

try {

rs = stmt.executeQuery(sql);

} catch (SQLException e) {

e.printStackTrace();

}

return rs;

}

public static ResultSet getPrepRset(PreparedStatement pstmt){

ResultSet rs = null;

try {

rs = pstmt.executeQuery();

} catch (SQLException e) {

e.printStackTrace();

}

return rs;

}

public static void close(Connection conn){

if(conn != null){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

conn = null;

}

}

public static void close(PreparedStatement pstmt){

if(pstmt != null){

try {

pstmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

pstmt = null;

}

}

public static void close(ResultSet rs){

if(rs != null){

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

rs = null;

}

}


}


jdbc连接Mysql方法封装,布布扣,bubuko.com

jdbc连接Mysql方法封装

标签:mysql   jdbc   

原文地址:http://yinzheyihao.blog.51cto.com/2100950/1538782

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!