• java 操作mysql数据库的代码


    显示某个数据库下的所有表

    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();
                }
            }
    
        }
  • 相关阅读:
    当统计信息不准确时,CBO可能产生错误的执行计划,并在10053 trace中找到CBO出错的位置示例
    ruby class_eval的使用
    ruby修改TXT文件
    ruby格式化
    VIM常用命令集合
    Watir::IE.attach与IE7选项卡的设置关系
    ruby 连接mysql数据库
    AutoIT删除Internet临时文件
    watir学习之—如何遍历页面所有的超链接
    watir如何取到元素的css属性
  • 原文地址:https://www.cnblogs.com/tomcattd/p/2842151.html
Copyright © 2020-2023  润新知