在JDBC上获取INSERT字符串

时间:2015-03-20 03:21:09

标签: sql oracle jdbc

我正在尝试编写一个方法来从SQL INSERT查询到DB获取字符串,输入数组作为使用JDBC的列数:

public String methodtest(String ListofColumns)
{
    String query = "INSERT INTO" + TableName + "(" + ListOfColumns + ")";
    try
    {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        While (rs.next())
        {
            String result = rs.getString(query);
            System.out.println("The SQL query is " + result);
        }
    }
    catch (SQLException sqlEx)
    {
        e.printStackTrace();
    }
    finally
    {
        rs.close();
    }
}

你能看到任何错误吗?

感谢Monkawee的建议。

1 个答案:

答案 0 :(得分:0)

您可以使用以下模式:

public string methodtest(Array listofcolumns)
{
    try{
        <Connection string path>
        <DB connection method>
        <Query string>
        <Execution>
        <Reset Object>
    }
    catch(SQLException sqlEx){
        // handle exception
    }
    catch(Exception ex){
        // handle other exceptions if any
    }
    finally{
        <Close connection and other resources like PreparedStatement>
    }
}