读取文件数据使用jdbc插入数据库

public final static String dbUrl                 = "jdbc:mysql://127.0.0.1:3306/test01";
    public final static String schemaName            = "test01";
    public final static String dbUsername            = "root";
    public final static String dbPassword            = "123456";

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
            Statement  s=  conn.createStatement();
            
            File file = new File("d:/", "coun.txt");  
            
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
            BufferedReader bf= new BufferedReader(isr);
            String  query = "";
            String line=null; 
               while((line = bf.readLine())!=null){
                String str[] = line.split(",");  
                if(str.length>1){
                    query="insert into customs_country (code,name) values (\""+str[0]+"\",\""+str[1]+"\");";
                    s.executeUpdate(query);  
                }
            }

            s.close();  
            conn.close();  
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

猜你喜欢

转载自wzw5433904.iteye.com/blog/2318809
今日推荐