您的位置:首页 > 数据库

如何在SSH环境下使用纯JDBC操作数据库

2012-08-31 17:47 465 查看
这是对数据库进行操作的DAO类
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import com.quickeditor.bean.ReaderBean;
import com.quickeditor.util.Algorithms;
import com.quickeditor.util.BaseDAO;

/**
* 纯JDBC操作的DAO
*
* @author Tang 2012/8/28
*/

public class ReaderDAO extends BaseDAO<ReaderBean> {

/**
* 清空一张数据表的数据
*
* @author Tang
* @param tableName
**/
public void truncateTable(String tableName) {
String sql = "truncate table " + tableName;

Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
try {
statement = connection.createStatement();
statement.execute(sql);
statement.close();
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

/**
* 给数据表增加一行数据
*
* @author Tang
* @param tableName,value[]
**/
public void saveRecord(String tableName, String[] value) {
String sql = "INSERT INTO " + tableName + " VALUES (";
for (int i = 0; i < value.length; i++) {
sql = sql + "'" + value[i] + "',";
}
sql = sql.substring(0, sql.length() - 1) + ")";

Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
statement.close();
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

/**
* 数据表删除数据列
*
* @author Tang
* @param tableName,columnName
**/
public void deleteColumn(String tableName, String columnName) {
String sql = "alter table " + tableName + " drop " + columnName;

Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
try {
statement = connection.createStatement();
statement.execute(sql);
statement.close();
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

/**
* 给数据表新增数据列
*
* @author Tang
* @param tableName ,columnName,type 支持的type(int,double,String,boolean)
**/
public void addColumn(String tableName, String columnName, String type) {
String sql = "";

if ("String".equals(type)) {
sql = "alter table " + tableName + " add " + columnName
+ " varchar(200)";
} else if ("int".equals(type)) {
sql = "alter table " + tableName + " add " + columnName + " int";
} else if ("double".equals(type)) {
sql = "alter table " + tableName + " add " + columnName
+ " double(10,3)";
} else if ("boolean".equals(type)) {
sql = "alter table " + tableName + " add " + columnName
+ " tinyint";
}

Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
try {
statement = connection.createStatement();
statement.execute(sql);
statement.close();
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

/**
* 新建数据表,带有一个默认的自增长的id列作为主键
*
* @author Tang
* @param tableName
**/
public void createTable(String tableName) {
String strSQL = "CREATE TABLE " + tableName
+ " (id INT(11) not null AUTO_INCREMENT,primary key (id));";
Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
try {
statement = connection.createStatement();
statement.execute(strSQL);
statement.close();
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

/**
* 查询某张表的所有列
*
* @author Tang
* @param tableName
*/
public List<String> queryTableColumns(String tableName){
String strSQL = "select COLUMN_NAME from information_schema.columns where table_name='"+tableName+"'";

Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
ResultSet rs = null;

List<String> columns=new ArrayList<String>();
try {
statement = connection.createStatement();
rs = statement.executeQuery(strSQL);
connection.commit();
while (rs.next()) {
columns.add(rs.getString(1));
}
}catch (SQLException e) {
System.out.println(e.getMessage());
}
return columns;
}

/**
* 查询数据表并把数据转换成一个json字符串
*
* @author Tang
* @param tableName,String[] columns
**/
public String queryTableForJson(String tableName, List<String> columns) {
String strSQL = "SELECT * FROM " + tableName;

Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
ResultSet rs = null;
String jsonValue = "[";
try {
statement = connection.createStatement();
rs = statement.executeQuery(strSQL);
connection.commit();
while (rs.next()) {
jsonValue = jsonValue + "{";
// rs是一条数据记录
for (int i = 0; i < columns.size(); i++) {
int index = i + 1;
String rec = rs.getString(columns.get(i));
String value = "";
if (rec == null || "".equals(rec)) {
value = "c" + index + ":'',";
} else {
if (Algorithms.isNum(rec)) {
value = "c" + index + ":" + rec + ",";
} else {
value = "c" + index + ":'" + rec + "',";
}
}
jsonValue = jsonValue + value;
}
jsonValue = jsonValue.substring(0, jsonValue.length() - 1);
jsonValue = jsonValue + "},";
}
connection.close();
statement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
if (!"[".equals(jsonValue)) {
jsonValue = jsonValue.substring(0, jsonValue.length() - 1) + "]";
}else{
jsonValue = jsonValue + "]";
}
return jsonValue;
}

/**
* 执行参数给定的sql语句
* 目前excel转换部分使用
* @param sql
*/
public void executeSQL(String sql){
Session session = super.getSession();
Connection connection = session.connection();
Statement statement;
try {
statement = connection.createStatement();
statement.execute(sql);
statement.close();
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jdbc ssh string session sql table