从 sql table 值中减去扫描仪值
Subtract scanner value from sql table value
我正在尝试从 table 值中减去扫描仪输入。这是我的 table:
的屏幕截图
我的代码如下所示:
String url = "jdbc:mysql://localhost:3306/Project";
String username = "x";
String password = "y";
try {
Scanner scanner = new Scanner(System.in);
System.out.println("What would you like to buy?");
int purchase_id = scanner.nextInt();
System.out.println("How many would you like to purchase?");
int quantity = scanner.nextInt();
Connection conn = DriverManager.getConnection(url, username, password);
Statement myStmt = conn.createStatement();
String sql = "update seattleBranch set inventory = " + quantity + "where item_id = 001";
myStmt = conn.prepareStatement(sql);
((PreparedStatement) myStmt).executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
我的代码的输出是:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'item_id = 001' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
at branchComms.main(branchComms.java:27)
这是我希望 table 在减去 100(我的扫描仪输入数量)后的样子:
我不确定为什么减去我的扫描仪变量(数量)不起作用,如果对我的代码进行任何更正,我将不胜感激。
您在 where
部分之前的查询中缺少 space,它应该像:
String sql = "update seattleBranch set inventory = " + quantity + " where item_id = 001";
我正在尝试从 table 值中减去扫描仪输入。这是我的 table:
的屏幕截图我的代码如下所示:
String url = "jdbc:mysql://localhost:3306/Project";
String username = "x";
String password = "y";
try {
Scanner scanner = new Scanner(System.in);
System.out.println("What would you like to buy?");
int purchase_id = scanner.nextInt();
System.out.println("How many would you like to purchase?");
int quantity = scanner.nextInt();
Connection conn = DriverManager.getConnection(url, username, password);
Statement myStmt = conn.createStatement();
String sql = "update seattleBranch set inventory = " + quantity + "where item_id = 001";
myStmt = conn.prepareStatement(sql);
((PreparedStatement) myStmt).executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
我的代码的输出是:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'item_id = 001' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
at branchComms.main(branchComms.java:27)
这是我希望 table 在减去 100(我的扫描仪输入数量)后的样子:
我不确定为什么减去我的扫描仪变量(数量)不起作用,如果对我的代码进行任何更正,我将不胜感激。
您在 where
部分之前的查询中缺少 space,它应该像:
String sql = "update seattleBranch set inventory = " + quantity + " where item_id = 001";