Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JDBC插入局部变量MySQL 公共类数据库{ 公共静态void main(字符串[]args){ try(Connection=DriverManager.getConnection(“jdbc:mysql://localhost:3306/company", )) { Type[]types={新图形卡()、新硬盘()、新键盘()、新内存()、新监视器()、新鼠标()、新处理器()}; 产品=新产品(10种类型); Range rangeUnitPrice=新的范围(10_000,220_000); 量程量程数量=新量程(0,20); 语句Statement=connection.createStatement(); while(product.getNumberOfEntry()>0){ 字符串类型和类别=product.getRandomType(); 字符串名称=product.getName(类型和类别); 字符串描述=product.getDescription(); 双单价=产品。随机单价生成器(名称,“x”,范围单价); int QUOTE_In_Stock=产品。生成等价物_In_Stock(范围数量); String brand=product.getRandomBrand(); System.out.println(“名称:“+名称+”,“+”类型:“+类型和类别+”,“+”随机价格:“+单价+”,“+”库存数量:“+库存数量+”,“+”随机品牌:“+品牌”); 字符串查询=“插入产品值(“+name+”、“+description+”、“+unit\u Price+”、“+quantity\u In\u Stock+”、“+brand+”、“+typeAndCatagory+”、“+typeAndCatagory+”)”; 语句。执行更新(查询); 产品。减少中心数量(); } }捕获(SQLE异常){ e、 printStackTrace(); } } }_Java_Mysql_Jdbc - Fatal编程技术网 0){ 字符串类型和类别=product.getRandomType(); 字符串名称=product.getName(类型和类别); 字符串描述=product.getDescription(); 双单价=产品。随机单价生成器(名称,“x”,范围单价); int QUOTE_In_Stock=产品。生成等价物_In_Stock(范围数量); String brand=product.getRandomBrand(); System.out.println(“名称:“+名称+”,“+”类型:“+类型和类别+”,“+”随机价格:“+单价+”,“+”库存数量:“+库存数量+”,“+”随机品牌:“+品牌”); 字符串查询=“插入产品值(“+name+”、“+description+”、“+unit\u Price+”、“+quantity\u In\u Stock+”、“+brand+”、“+typeAndCatagory+”、“+typeAndCatagory+”)”; 语句。执行更新(查询); 产品。减少中心数量(); } }捕获(SQLE异常){ e、 printStackTrace(); } } },java,mysql,jdbc,Java,Mysql,Jdbc" /> 0){ 字符串类型和类别=product.getRandomType(); 字符串名称=product.getName(类型和类别); 字符串描述=product.getDescription(); 双单价=产品。随机单价生成器(名称,“x”,范围单价); int QUOTE_In_Stock=产品。生成等价物_In_Stock(范围数量); String brand=product.getRandomBrand(); System.out.println(“名称:“+名称+”,“+”类型:“+类型和类别+”,“+”随机价格:“+单价+”,“+”库存数量:“+库存数量+”,“+”随机品牌:“+品牌”); 字符串查询=“插入产品值(“+name+”、“+description+”、“+unit\u Price+”、“+quantity\u In\u Stock+”、“+brand+”、“+typeAndCatagory+”、“+typeAndCatagory+”)”; 语句。执行更新(查询); 产品。减少中心数量(); } }捕获(SQLE异常){ e、 printStackTrace(); } } },java,mysql,jdbc,Java,Mysql,Jdbc" />

Java 如何使用JDBC插入局部变量MySQL 公共类数据库{ 公共静态void main(字符串[]args){ try(Connection=DriverManager.getConnection(“jdbc:mysql://localhost:3306/company", )) { Type[]types={新图形卡()、新硬盘()、新键盘()、新内存()、新监视器()、新鼠标()、新处理器()}; 产品=新产品(10种类型); Range rangeUnitPrice=新的范围(10_000,220_000); 量程量程数量=新量程(0,20); 语句Statement=connection.createStatement(); while(product.getNumberOfEntry()>0){ 字符串类型和类别=product.getRandomType(); 字符串名称=product.getName(类型和类别); 字符串描述=product.getDescription(); 双单价=产品。随机单价生成器(名称,“x”,范围单价); int QUOTE_In_Stock=产品。生成等价物_In_Stock(范围数量); String brand=product.getRandomBrand(); System.out.println(“名称:“+名称+”,“+”类型:“+类型和类别+”,“+”随机价格:“+单价+”,“+”库存数量:“+库存数量+”,“+”随机品牌:“+品牌”); 字符串查询=“插入产品值(“+name+”、“+description+”、“+unit\u Price+”、“+quantity\u In\u Stock+”、“+brand+”、“+typeAndCatagory+”、“+typeAndCatagory+”)”; 语句。执行更新(查询); 产品。减少中心数量(); } }捕获(SQLE异常){ e、 printStackTrace(); } } }

Java 如何使用JDBC插入局部变量MySQL 公共类数据库{ 公共静态void main(字符串[]args){ try(Connection=DriverManager.getConnection(“jdbc:mysql://localhost:3306/company", )) { Type[]types={新图形卡()、新硬盘()、新键盘()、新内存()、新监视器()、新鼠标()、新处理器()}; 产品=新产品(10种类型); Range rangeUnitPrice=新的范围(10_000,220_000); 量程量程数量=新量程(0,20); 语句Statement=connection.createStatement(); while(product.getNumberOfEntry()>0){ 字符串类型和类别=product.getRandomType(); 字符串名称=product.getName(类型和类别); 字符串描述=product.getDescription(); 双单价=产品。随机单价生成器(名称,“x”,范围单价); int QUOTE_In_Stock=产品。生成等价物_In_Stock(范围数量); String brand=product.getRandomBrand(); System.out.println(“名称:“+名称+”,“+”类型:“+类型和类别+”,“+”随机价格:“+单价+”,“+”库存数量:“+库存数量+”,“+”随机品牌:“+品牌”); 字符串查询=“插入产品值(“+name+”、“+description+”、“+unit\u Price+”、“+quantity\u In\u Stock+”、“+brand+”、“+typeAndCatagory+”、“+typeAndCatagory+”)”; 语句。执行更新(查询); 产品。减少中心数量(); } }捕获(SQLE异常){ e、 printStackTrace(); } } },java,mysql,jdbc,Java,Mysql,Jdbc,查询不起作用,第一个值是默认值(主键自动递增),我不需要指定。错误如下 java.sql.SQLSyntaxErrorException:您的sql中有一个错误 句法;检查与MySQL服务器版本对应的手册 对于正确的语法使用近'10 AMD图形卡游戏版, 180657.63138583858,6,HP,图形卡,第1行的Gr' 您可以格式化此行中的字符串以用作SQL语句: String query = "INSERT INTO product VALUES (" + name

查询不起作用,第一个值是默认值(主键自动递增),我不需要指定。错误如下


java.sql.SQLSyntaxErrorException:您的sql中有一个错误 句法;检查与MySQL服务器版本对应的手册 对于正确的语法使用近'10 AMD图形卡游戏版, 180657.63138583858,6,HP,图形卡,第1行的Gr'


您可以格式化此行中的字符串以用作SQL语句:

String query = "INSERT INTO product VALUES (" + name + ", " + description + ", " + unit_Price + ", " + quantity_In_Stock + ", " + brand + ", " + typeAndCatagory + ", " + typeAndCatagory + ")";
此语句有问题,导致它产生语法错误。怎么了

通过查看Java表达式很难调试它。查看所有的
+
并查看出了什么问题是令人困惑的

如果可以看到字符串的最终结果,而不是构建字符串的Java表达式,那么就更容易看到错误所在

因此,在执行之前,请尝试将其打印出来:

System.out.println(query);
这样,问题可能会更加明显

我预测它会像这样:

INSERT INTO product VALUES (10 AMD graphic card Gamer Edition, , 180657.63138583858, 6, HP, Graphic Card, Gr...
您的values子句中的字符串值缺少引号。这是无效的SQL

最好的解决方案是学会使用查询参数。这样你就不必担心值周围的引号。而且代码在SQL注入中更安全

在您的情况下,类似于以下内容:

String query = "INSERT INTO product VALUES (?, ?, ?, ?, ?, ?, ?)";
Statement statement = connection.prepareStatement(query);
while (product.getNumberOfEntery() > 0) {

    // set the values for all your variables...

    statement.setString(1, name);
    statement.setString(2, description);
    statement.setDouble(3, unit_Price);
    statement.setInt(4, quantity_In_Stock);
    statement.setString(5, brand);
    statement.setString(6, typeAndCatagory);
    statement.setString(7, typeAndCatagory);
    statement.executeUpdate();
}

您可以格式化此行中的字符串以用作SQL语句:

String query = "INSERT INTO product VALUES (" + name + ", " + description + ", " + unit_Price + ", " + quantity_In_Stock + ", " + brand + ", " + typeAndCatagory + ", " + typeAndCatagory + ")";
这个语句有点问题,导致它产生语法错误。怎么了

通过查看Java表达式很难调试它。查看所有的
+
并查看出了什么问题是令人困惑的

如果可以看到字符串的最终结果,而不是构建字符串的Java表达式,那么就更容易看到错误所在

因此,在执行之前,请尝试将其打印出来:

System.out.println(query);
这样,问题可能会更加明显

我预测它会像这样:

INSERT INTO product VALUES (10 AMD graphic card Gamer Edition, , 180657.63138583858, 6, HP, Graphic Card, Gr...
您的values子句中的字符串值缺少引号。它不是有效的SQL

最好的解决方案是学习使用查询参数。那么你就不必担心围绕值的引号了。而且代码在SQL注入中更加安全

在您的情况下,类似于以下内容:

String query = "INSERT INTO product VALUES (?, ?, ?, ?, ?, ?, ?)";
Statement statement = connection.prepareStatement(query);
while (product.getNumberOfEntery() > 0) {

    // set the values for all your variables...

    statement.setString(1, name);
    statement.setString(2, description);
    statement.setDouble(3, unit_Price);
    statement.setInt(4, quantity_In_Stock);
    statement.setString(5, brand);
    statement.setString(6, typeAndCatagory);
    statement.setString(7, typeAndCatagory);
    statement.executeUpdate();
}

您的代码有两个问题:

  • 主要的一点是,您的代码容易受到攻击
  • 您必须自己用单引号将文本值括起来
  • 这两个问题的解决方案如下所示:

    String query = "INSERT INTO product VALUES (?, ?, ?, ?, ?)";
    
    try (PreparedStatement pstmt = con.prepareStatement(query)) {
        //...
        
        pstmt.setString(1, name);
        pstmt.setString(2, description);
        pstmt.setDouble(3, unit_Price);
        //...
    
        pstmt.executeUpdate();
    }
    

    此外,我建议您始终遵循,例如,
    单价
    应命名为
    单价

    您的代码有两个问题:

  • 主要的一点是,您的代码容易受到攻击
  • 您必须自己用单引号将文本值括起来
  • 这两个问题的解决方案如下所示:

    String query = "INSERT INTO product VALUES (?, ?, ?, ?, ?)";
    
    try (PreparedStatement pstmt = con.prepareStatement(query)) {
        //...
        
        pstmt.setString(1, name);
        pstmt.setString(2, description);
        pstmt.setDouble(3, unit_Price);
        //...
    
        pstmt.executeUpdate();
    }
    

    此外,我建议您始终遵循,例如,
    单价
    应命名为
    单价

    “查询不起作用”-“不起作用”是什么意思?请回答问题并描述预期和观察到的行为。java.sql.SQLSyntaxErrorException:您的sql语法有错误;请查看与MySQL服务器版本对应的手册,以了解使用第1行“10 AMD graphic card Gamer Edition,180657.63138583858,6,HP,graphic card,Gr”的正确语法请回答您的问题…使用绑定参数。看看你的问题。你的价值是什么。@ElliottFrisch你能解释一下吗,我不明白你说的“查询不起作用”是什么意思?什么是“不起作用”?请回答问题并描述预期和观察到的行为。java.sql.SQLSyntaxErrorException:您的sql语法有错误;请查看与MySQL服务器版本对应的手册,以了解使用第1行“10 AMD graphic card Gamer Edition,180657.63138583858,6,HP,graphic card,Gr”的正确语法请回答您的问题…使用绑定参数。看看你的问题。一个你的值什么都不是。@ElliottFrisch你能解释一下吗,我不明白你的意思很多thx对于你的答案,如果第一个元素是主键自动递增put会发生什么?或者不要放anything@MohrezaKh-不要