首页 > 数据库技术 > 详细

java mysql模板

时间:2014-05-11 15:50:46      阅读:446      评论:0      收藏:0      [点我收藏+]

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模板

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!