浏览 2600 次
锁定老帖子 主题:应用Hashtable提高插入数据的效率
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-04-18
问题:有一个表TABLE T id phone infomation …… 1 13341184545 AAA 2 13541541245 BBB .. .. .. .. .. .. 有一个tel_province.txt文件,格式如下 辽宁,沈阳,1334512 吉林,长春,1594512 .. .. .. .. .. .. 要求:在表一中增加字段province,city,并且根据tel_province.txt中手机号码的前7位与表一中的phone匹配后插入province,city这两个字段的所有信息。 我的问题:1。用sql语句很简单,但是效率很低,几十万条的数据数据库不能承受; 2。我是用java进行数据库编程的,代码如下,但是执行的时候, info[0],info[1],info[2]中出现nullpointException,不知道原因; 3。如何在Hashtable中做循环,每次匹配的时候,只执行一个update语句来更新信息,如果照我刚才的做法,会执行很多次的update语句,效率也不是很高。 4。主要寻求我刚才的错误原因,还有应用Hashtable做循环来更新数据库的方法。 5。代码如下,谢谢指教; import java.sql.*; public class DB { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private DB() {} public static Connection getConn() { Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_movie?user=root&password=root"); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void closeConn(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static Statement getStmt(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static PreparedStatement getPStmt(Connection conn, String sql) { PreparedStatement pStmt = null; try { pStmt = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pStmt; } public static PreparedStatement getPStmt(Connection conn, String sql, boolean generatedKey) { PreparedStatement pStmt = null; try { pStmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); } catch (SQLException e) { e.printStackTrace(); } return pStmt; } public static void closeStmt(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static ResultSet executeQuery(Statement stmt , String sql) { ResultSet rs = null; try { rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void closeRs(ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } public static ResultSet executeQuery(Connection conn, String sql) { ResultSet rs = null; try { rs = conn.createStatement().executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void executeUpdate(Connection conn, String sql) { try { conn.createStatement().executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } } /*---------------------*/ /*---------------------*/ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.sql.*; import java.util.Hashtable; import java.util.Map; import com.mno5.db.*; public class alter_log_spmo_200704 { /** * @param args */ public static void main(String[] args) { Hashtable hs = queryFilterInfo(); Connection conn = null; ResultSet rs = null; String sql_1 = "select * from log_spmo_200704"; try{ conn = DB.getConn(); System.out.println("已经连接到数据库"); rs = DB.executeQuery(conn, sql_1); while(rs.next()) { String id = rs.getString("id"); if(rs.getString(2).length()>=7){ String prefix = rs.getString(2).substring(0,7); String info[] = (String[])hs.get(prefix); System.out.print(info[0]); System.out.print(info[1]); System.out.println(prefix); if(info != null){ String sql_2 = "update log_spmo_200704 set province=" + info[0] + ',' + "log_spmo_200704.city=" + info[1] + " where id="+id; DB.executeUpdate(conn, sql_2); System.out.println(sql_2); System.out.println("正在更新表中数据"); } } } }catch(SQLException e1) { System.out.println("修改表失败"); } System.out.println("修改表成功"); DB.closeRs(rs); DB.closeConn(conn); } public static Hashtable queryFilterInfo() { //Vector vMsg = new Vector(); String filename = "tel_province.txt"; Hashtable ht = new Hashtable(); BufferedReader reader; try { File logFilef = new File("E:\\工作资料\\work\\tel_province.txt"); reader = new BufferedReader(new FileReader(logFilef)); } catch (Exception ex) { System.out.println("创建/打开log文件" + filename + "出错!"); return ht; } String line; try { while( (line=reader.readLine())!=null ) { String info[] = line.split(","); ht.put(info[2],info); //vMsg.add( line ); } reader.close(); }catch( Exception ex ) { System.out.println("读取log文件" + filename + "出错!"); return ht; } return ht; //return vMsg; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |