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

java mysql模板

时间:2014-05-11 15:51:27      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:class   java   c   int   get   string   

Java mysql的模版,很优雅。同时也兼顾了性能PreparedStatement和安全性(防SQL注入)两方面。对于比较简单的数据库操作基本满足要求。

package dao;

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

public class BaseDao {
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
public static final String USERNAME = "ma";
public static final String PASSWORD = "malei";

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public Connection getConnection() throws Exception {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return connection;
}

public ResultSet executeQuery(String sql) throws Exception {
connection = this.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
return resultSet;

}

public int executeUpdate(String sql,Object[] obj) throws Exception {
connection = this.getConnection();
preparedStatement = connection.prepareStatement(sql);
for(int i =0;i<obj.length;i++){
preparedStatement.setObject(i+1, obj[i]);
}
return preparedStatement.executeUpdate();
}

public void closeAll() throws Exception {
if(null != resultSet){
resultSet.close();
}
if(null != preparedStatement){
preparedStatement.close();
}
if(null != connection){
connection.close();
}
}
}

java mysql模板,布布扣,bubuko.com

java mysql模板

标签:class   java   c   int   get   string   

原文地址:http://www.cnblogs.com/hsqblogs/p/3721653.html

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