java 操作mysql数据库的代码
2013-01-02 15:37
399 查看
显示某个数据库下的所有表
给某个表添加一个列:
对list类型的数据插入到数据库中,批处理
更新数据库例子
public List<String> showTables() { List<String> tables = new ArrayList<String>(); try { PreparedStatement stmt = null; Connection conn = null; conn = ConnectionTools.getConn(); conn.setAutoCommit(false); String sql = "show tables"; stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { String table = rs.getString(1); tables.add(table); } conn.commit(); conn.close(); stmt.close(); } catch (Exception e) { e.printStackTrace(); } return tables; }
给某个表添加一个列:
public void crateCloum() { List<String> tables = showTables(); for (String string : tables) { try { PreparedStatement stmt = null; Connection conn = null; conn = ConnectionTools.getConn(); conn.setAutoCommit(false); String sql = "alter table " + string + " add mobileLinks varchar(200) null"; stmt = conn.prepareStatement(sql); stmt.executeUpdate(); conn.commit(); conn.close(); stmt.close(); } catch (Exception e) { e.printStackTrace(); } } }
对list类型的数据插入到数据库中,批处理
public void addCanshu(List<Canshu> list) { // System.out.println(list.size()); Connection conn = ConnectionTools.getConn(); try { conn.setAutoCommit(false); String sql = "insert canshu(urlId,firstClass,canshukey,canshuvalue) values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); for (Canshu product : list) { ps.setString(1, product.getUrlID()); ps.setString(2, product.getFirstClass()); ps.setString(3, product.getKey()); ps.setString(4, product.getValue()); ps.addBatch(); } ps.executeBatch(); conn.commit(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { try { ConnectionTools.close(); } catch (Exception e) { e.printStackTrace(); } } }
更新数据库例子
public void updateCanmore() { List<Canshu> tables = getCanshuInSql(); for (Canshu c : tables) { try { PreparedStatement stmt = null; Connection conn = null; conn = ConnectionTools.getConn(); conn.setAutoCommit(false); String sql = "update canmore set " + c.getKey() + "= '" + c.getValue().trim() + "' where urlId=" + c.getUrlID(); System.out.println(sql); stmt = conn.prepareStatement(sql); stmt.execute(); conn.commit(); conn.close(); stmt.close(); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- Java连接MySQL数据库及简单操作代码
- java使用jdbc对mysql数据库进行操作的步骤和代码
- 非常短的java操作mysql数据库的代码
- 【Java】利用单例模式、可变参数优化Java操作Mysql数据库、JDBC代码的写作
- Java连接MySQL数据库及简单操作代码
- Java语言实现对MySql数据库中数据的增删改查操作的代码
- Java连接到MySQL数据库的代码及相关操作
- Java简单操作mysql数据库代码存档+处理中文乱码问题
- Java连接MySQL数据库及简单操作代码
- java 发送邮件 email相关操作代码测试,生成复杂格式邮件,发送邮件相关操作
- 【Java】【MySQL】Java操作mysql数据库基本步骤介绍——从零开始#1
- Java 第二次作业:MySQL数据库及Java操作MySQL数据库
- java io操作代码
- Java文件读写操作具体代码实现
- java代码LDAP操作(增删改查)AD(活动目录)- 添加
- JDBC java对MySQL数据库进行查询操作,并把查询的结果输出
- 最简单的使用mysql数据库的Java代码
- JAVA中通过代码操作PC内容进行功能的实现
- Java文件IO操作工具类(复制、删除、重命名、创建路径),复制代码并创建FileControl类即可使用。
- java连接mysql数据库 三 实现增删改查操作