有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    给你:

    String sql = "insert into table1 select * from table2 [where conditions]";
    Statement stmt = connection.prepareStatement(sql);
    stmt.executeUpdate();
    
  2. # 2 楼答案

    你可以试试这样的东西:-

     Statement st1 = con1.createStatement();
     ResultSet rs = st1.executeQuery("select * from table1");
     PreparedStatement ps = null;
    
     while(rs.next())
        {
            ps = con2.prepareStatement("insert into table2 values(?,?)");
            ps.setInt(rs.getInt());
            ps.setString(rs.getString());
            ps.executeUpdate();
        }
    

    假设表中有两列

  3. # 3 楼答案

    您可以为此使用PreparedStations

    示例代码在这里

     int result;
        Connection connection = null; // manages connection
                PreparedStatement insert = null;
    
        connection = DriverManager.getConnection(DB_URL,"root","password");
                    insert = connection.prepareStatement("INSERT into table2" +
                    "(f1,f2,f3)" +
                    "VALUES(?,?,?)" );
                            insert.setString(1, Yourvaluehere);
                    insert.setInt(2, Yourvaluehere);
                    insert.setDouble(3, YourValueHere);
        result = insert.executeUpdate();
    

    //Yourvaluehere-应该来自对象的getmethods-这就是我所做的,如果你有这个想法,你可以做得更好