Inserting records into a MySQL table using Java

喜你入骨 提交于 2019-12-03 12:26:12

no that cannot work(not with real data):

String sql = "INSERT INTO course " +
        "VALUES (course_code, course_desc, course_chair)";
    stmt.executeUpdate(sql);

change it to:

String sql = "INSERT INTO course (course_code, course_desc, course_chair)" +
        "VALUES (?, ?, ?)";

Create a PreparedStatment with that sql and insert the values with index:

PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "Test2");
preparedStatement.setString(3, "Test3");
preparedStatement.executeUpdate(); 

this can also be done like this if you don't want to use prepared statements.

String sql = "INSERT INTO course(course_code,course_desc,course_chair)"+"VALUES('"+course_code+"','"+course_desc+"','"+course_chair+"');"

Why it didnt insert value is because you were not providing values, but you were providing names of variables that you have used.

There is a mistake in your insert statement chage it to below and try : String sql = "insert into table_name values ('" + Col1 +"','" + Col2 + "','" + Col3 + "')";

This should work for any table, instead of hard-coding the columns.

//Source details
    String sourceUrl = "jdbc:oracle:thin:@//server:1521/db";
    String sourceUserName = "src";
    String sourcePassword = "***";

    // Destination details
    String destinationUserName = "dest";
    String destinationPassword = "***";
    String destinationUrl = "jdbc:mysql://server:3306/db";

    Connection srcConnection = getSourceConnection(sourceUrl, sourceUserName, sourcePassword);
    Connection destConnection = getDestinationConnection(destinationUrl, destinationUserName, destinationPassword);

    PreparedStatement sourceStatement = srcConnection.prepareStatement("SELECT *  FROM src_table ");
    ResultSet rs = sourceStatement.executeQuery();
    rs.setFetchSize(1000); // not needed


    ResultSetMetaData meta = rs.getMetaData();



    List<String> columns = new ArrayList<>();
    for (int i = 1; i <= meta.getColumnCount(); i++)
        columns.add(meta.getColumnName(i));

    try (PreparedStatement destStatement = destConnection.prepareStatement(
            "INSERT INTO dest_table ("
                    + columns.stream().collect(Collectors.joining(", "))
                    + ") VALUES ("
                    + columns.stream().map(c -> "?").collect(Collectors.joining(", "))
                    + ")"
            )
    )
    {
        int count = 0;
        while (rs.next()) {
            for (int i = 1; i <= meta.getColumnCount(); i++) {
                destStatement.setObject(i, rs.getObject(i));
            }
            
            destStatement.addBatch();
            count++;
        }
        destStatement.executeBatch(); // you will see all the rows in dest once this statement is executed
        System.out.println("done " + count);

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!