首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从一个Mysql数据库插入到另一个数据库

问从一个Mysql数据库插入到另一个数据库
EN

Stack Overflow用户
提问于 2014-03-20 01:18:41
回答 2查看 81关注 0票数 0

我正在尝试将数据从一个mysql数据库传输到另一个.For数据库,我有连接url作为数据库名称,用户名和密码(因为这必须存在于远程位置).And为另一个我正在使用数据源。

代码语言:javascript
复制
public class errorlog {
     public static void main(String[] args)
        {
            try {
              Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded mysql1server");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        System.out.println("Driver loaded mysql");

            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("not loaded");
            }
            String connectionUrl = "jdbc:mysql//localhost:3306/db1" ;

              String connectionUrl2="jdbc:odbc:test";
                Connection con=null;
                Connection con2=null;
                Statement stmt = null;
                ResultSet rs = null;
                PreparedStatement pstmt = null;


                try {
                    con = DriverManager.getConnection(connectionUrl,"root","root");
                    System.out.println("CONNECTED1 mysql1");
                 con2 = DriverManager.getConnection(connectionUrl2);
              System.out.println("CONNECTED2 mysql");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                try {
                 stmt = con.createStatement();
                 rs=stmt.executeQuery("select * FROM db1.db1_table");
                  System.out.print("Select executed");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


               try {
                    while (rs.next()) {

                        int col1 = rs.getInt("col1");
                        String col2=rs.getString("col2");


Date TimeStamp = rs.getDate("Time Stamp");



                    String insertSql ="Insert into db2.db2_table (`col1`,`col2`) values(?,?)";
                            pstmt = con2.prepareStatement(insertSql);

                             pstmt.setInt(1,col1);
                            pstmt.setString(2,col2);


pstmt.setDate(3,(java.sql.Date) TimeStamp);

                            pstmt.executeUpdate();




                       pstmt.close();
                       System.out.println("insert done");
                    }

                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                System.out.println("out insert done");


    }
    }

我收到一个错误,因为“找不到适用于jdbc:mysql//localhost:3306/databaseName=db1;user=root;password=root”的驱动程序

请告诉我我哪里错了。

进一步的要求-

我想使用一个配置文件,它将持有一个时间值,并将不断更新,因为数据是获取。例如-文件持有值2014-03-18 12:00:00,程序将在1小时的周期性间隔运行.I想要获取数据后,只有在文件持有时间.And一旦运行,文件应该更新。

请让我知道我怎么做dat。

EN

回答 2

Stack Overflow用户

发布于 2014-03-20 01:23:15

您没有加载MySQL驱动程序。你得到了ODBC驱动程序

代码语言:javascript
复制
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

但是你需要mysql jdbc协议的一些东西,例如

代码语言:javascript
复制
Class.forName("com.mysql.jdbc.Driver");

然后需要在项目中引用/包含mysql连接器jar。

票数 0
EN

Stack Overflow用户

发布于 2014-03-20 01:50:31

我收到一个错误,因为“找不到适用于jdbc:mysql//localhost:3306/databaseName=db1;user=root;password=root”的驱动程序

‘`mysql’后面需要一个冒号

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22513427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档