如何在MySQL中的表中插入变量 [英] how to insert variable into a table in MySQL

查看:205
本文介绍了如何在MySQL中的表中插入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to add data into a table. Like

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

11.1和12.1可以插入表中.现在给定一个float变量

11.1 and 12.1 can be inserted into table. Now given a float variable

float fv = 11.1;

如何将fv插入表中?

推荐答案

在JAVA中,您可以使用如下准备好的语句:

In JAVA, you can use prepared statements like this:

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();
}

这篇关于如何在MySQL中的表中插入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆