Java程序两个数据表之间导数据mysql

来源:互联网 发布:天龙八部全套源码资源 编辑:程序博客网 时间:2024/04/30 14:31


最近写的一个Java程序

需求:两个数据表之间导数据mysql

           修复bug后把旧的数据导入到新的数据表中

           在服务器端或本地跑一下这个程序,就能完成这个需求;


package com.ibatis.test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;import com.cc.ovp.domain.PlayerSkin;import com.cc.ovp.domain.PlayerSkin.Ext;public class PlayerSkinInputHostSetting {        private static Connection connection;     private static Statement statement;     private static ResultSet resultSet;     private ResultSetMetaData rsMetaData;            ////构造函数    public PlayerSkinInputHostSetting(){                String url = "jdbc:mysql://192.168.168.20:3306/web";         String username = "xuan";         String password = "123456";         //加载驱动程序以连接数据库        try {             Class.forName( "org.gjt.mm.mysql.Driver" );             connection = DriverManager.getConnection(url, username, password );         }         //捕获加载驱动程序异常        catch ( ClassNotFoundException cnfex ) {             System.err.println("装载 JDBC/ODBC 驱动程序失败");             cnfex.printStackTrace();         }         //捕获连接数据库异常        catch ( SQLException sqlex ) {             System.err.println( "无法连接数据库" );             sqlex.printStackTrace();        }               }            ///////////////////////////////    private static void getTable()     {     try {         //执行SQL语句        //String query = inputQuery.getText();         statement = connection.createStatement();         resultSet = statement.executeQuery("select * from player_skin;");         //在表格中显示查询结果         //DOC[] docs= resultSet;     /*   List list=(List)resultSet;        PlayerSkin palyerSkin=new PlayerSkin();         System.out.println("记录数===="+list.size());        for(int i=0; i<list.size();i++){            System.out.println(i+"========"+palyerSkin.getPlayerid()+"====="+palyerSkin.getUserid());        }*/        PlayerSkin palyerSkin2=new PlayerSkin();         PlayerSkin.Ext ext=null;        int j=0;        while(resultSet.next()){            j++;            String extTo=resultSet.getString("ext");           //System.out.println("======"+extTo);                try {                    ext=(PlayerSkin.Ext.fromJSON(extTo));                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                        System.out.println(j+"========"+resultSet.getString("playerid")                    +"====="+resultSet.getString("userid")                    +"===D==="+ext.getDisable_host()                    +"===E==="+ext.getEnable_host());                    //+"==EXT==="+resultSet.getString("ext"));                                  //System.out.println(j+"=========="+ext.getDisable_host()+"=========="+ext.getEnable_host());          insertDate(resultSet.getString("userid"), ext.getEnable_host(), ext.getDisable_host());        }        resultSet.close();        statement.close();        //connection.close();           }     catch ( SQLException sqlex ) {           sqlex.printStackTrace();        }    }         ////插入数据    public static void insertDate(String userid,String enable_host,String disable_host) throws SQLException{        //statement=connection.createStatement();        PreparedStatement ps=connection.prepareStatement("insert into host_setting(userid,enable_host,disable_host,setting_type) values(?,?,?,?)");        ps.setString(1, userid);        ps.setString(2, enable_host);        ps.setString(3, disable_host);        ps.setInt(4, 0);                ps.executeUpdate();        ps.close();            }                public  static void main(String[] args){        PlayerSkinInputHostSetting psh=new PlayerSkinInputHostSetting();        psh.getTable();         /*       try {            String userid="1234";            String enable_host="hao123.com";            String disable_host="xixi.com";            psh.insertDate(userid, enable_host, disable_host);            connection.close();            System.out.println("=======导入成功===");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        */    }}


原创粉丝点击