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

这是对数据库进行操作的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());
		}
	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值