如何将变量插入MySQL中的表中

wzb*_*210 1 java jdbc insert prepared-statement

我知道如何将数据添加到表中.喜欢

String insertQuery = "INSERT INTO tablename (x_coord, y_coord)"
                            +"VALUES"
                            +"(11.1,12.1)";
s.execute(insertQuery);
Run Code Online (Sandbox Code Playgroud)

11.1和12.1可以插入表中.现在给出一个浮点变量

float fv = 11.1;
Run Code Online (Sandbox Code Playgroud)

如何将fv插入表中?

Emr*_*ici 6

在JAVA中,您可以使用这样的预准备语句:

Connection conn = null;
PreparedStatement pstmt = null;

float floatValue1 = 11.1;
float floatValue2 = 12.1;

try {
    conn = getConnection();
    String insertQuery = "INSERT INTO tablename (x_coord, y_coord)"
                        +"VALUES"
                        +"(?, ?)";
    pstmt = conn.prepareStatement(insertQuery);
    pstmt.setFloat(1, floatValue1);
    pstmt.setFloat(2, floatValue2);
    int rowCount = pstmt.executeUpdate();
    System.out.println("rowCount=" + rowCount);
} finally {
    pstmt.close();
    conn.close();
}
Run Code Online (Sandbox Code Playgroud)