MySQL查询INSERT in Java

时间:2015-11-24 09:44:01

标签: mysql jdbc netbeans

我正在使用Netbeans,在这个方法中我有som String和int变量,我想在MySQL数据库中插入。

我试过这个,但它仍然没有用。

String sql = "INSERT INTO cars (Manufacturer, Color, Extra, Country, Price)" "VALUES (variableManufacturer, variableColor, variableExtra, variableCountry, variablePrice)";

表名汽车

Colummns Manufacturer, Color, Extra, Country, Price

  

所有colummns都是VARCHAR,除了Price,它是一个int。

Netbeans崩溃了,它说无法找到变量。任何人吗?

4 个答案:

答案 0 :(得分:0)

试试这个

String sql = "INSERT INTO cars(Manufacturer, Color, Extra, Country, Price) VALUES(variableManufacturer, variableColor, variableExtra, variableCountry, variablePrice)";

<强>更新

我认为此链接可以帮助您

Inserting records into a MySQL table using Java

答案 1 :(得分:0)

试试这个:

String sql = "INSERT INTO cars (Manufacturer, Color, Extra, Country, Price)";

sql + = "  VALUES (variableManufacturer, variableColor, variableExtra, variableCountry, variablePrice)";

答案 2 :(得分:0)

试试这个:)

String sql = "INSERT INTO cars (Manufacturer, Color, Extra, Country, Price) "
+ "VALUES ('"+variableManufacturer+"', '"+variableColor+"',
           '"+variableExtra+"', '" +variableCountry +"', 
           "+variablePrice+")";

答案 3 :(得分:0)

您正在寻找的是Java中的PreparedStatement

喜欢的东西;

PreparedStatement ps = connection.prepareStatement("INSERT INTO cars (Manufacturer, Color, Extra, Country, Price) VALUES (?,?,?,?,?)");

然后,您将ps.SetXX()方法与变量一起使用,在您的情况下为VARCHAR我认为属于String转化类型:JDBC - How to set char in a prepared statement