如何在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()); } } }
相关文章推荐
- Java - 使用JDBC操作数据库时,如何提升读取数据的性能?如何提升更新数据的性能?
- 使用jdbc更新数据时间时如何屏蔽各种数据库的差异性
- 使用XAMPP可视化管理Mysql,使用JDBC访问数据库执行插入、查询、删除等操作
- Java使用JDBC操作数据库
- 使用jdbc操作数据库
- 使用JDBC进行数据库的事务操作(2)
- 【iOS10】使用XCode8、Swift3. 操作SQLite3数据库的步骤(包括配置环境和SQLite语句)
- JDBC数据库的使用操作总结
- 如何使用ANT自动进行数据库的相关操作
- Spring 使用原生的jdbc或整合ORM操作数据库的模板编程
- 如何使用三层框架开发 JDBC 连接 Mysql 数据库
- 使用JDBC进行数据库的事务操作(2)
- 浅谈如何使用ADO.NET操作数据库
- java使用jdbc操作数据库示例分享
- ★ 浅谈如何使用ADO.NET操作数据库 ★
- 如何使用Jdbc和Servlet操作Mysql数据库,编写Android登录注册服务端?
- 如何在高并发环境下设计出无锁的数据库操作(Java版本) 转载
- 使用SSH在做插入和修改的时候即使数据库中给字段设置了默认值还是没有用,在表对应的配置文件中也不知道该如何配置。。请教下。。
- Java中使用JDBC连接数据库操作的步骤
- JDBC的使用五大步骤以及查询操作-数据库编程(二)