Java jdbc错误,无法连接到sql
我想在我的项目中使用数据库,然后我使用这段代码进行测试(来自jdbc) 并为我的代码和数据库更改它 然后我得到这个错误:Java jdbc错误,无法连接到sql,java,database,jdbc,Java,Database,Jdbc,我想在我的项目中使用数据库,然后我使用这段代码进行测试(来自jdbc) 并为我的代码和数据库更改它 然后我得到这个错误: Creating statement... 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 'FROM test SET name=eee WHERE id
Creating statement...
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 'FROM test SET name=eee WHERE id=1' at line 1
Error: unable to connect to SQL!
java.sql.SQLException: 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 'FROM test SET name=eee WHERE id=1' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3020)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2949)
at com.mysql.jdbc.Statement.execute(Statement.java:538)
at Test.main(Test.java:49)
我的代码:
import java.sql.*;
import java.math.*;
public class Test {
final static String DB_URL = "jdbc:mysql://localhost/testdb";
final static String USER = "root";
final static String PASS = "";
final static String JDBC_DRIVER="com.mysql.jdbc.Driver";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "UPDATE name FROM test SET name=eee WHERE id=1";
Boolean ret = stmt.execute(sql);
System.out.println("Return value is : " + ret.toString() );
int rows = stmt.executeUpdate(sql);
System.out.println("Rows impacted : " + rows );
sql = "SELECT id,name FROM test";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.print("ID: " + id);
System.out.print(", name: " + name);
}
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException ex) {
ex.printStackTrace();
System.out.println("\n" + ex.getMessage());
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
catch(IllegalAccessException ex) {
ex.printStackTrace();
System.out.println("\n" + ex.getMessage());
System.out.println("Error: access problem while loading!");
System.exit(2);
}
catch(InstantiationException ex) {
ex.printStackTrace();
System.out.println("\n" + ex.getMessage());
System.out.println("Error: unable to instantiate driver!");
System.exit(3);
}
catch (SQLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
System.out.println("\n" + ex.getMessage());
System.out.println("Error: unable to connect to SQL!");
System.exit(4);
}
}
}
我的数据库是:
我看到了
但这对我没有帮助 Change
String sql=“从测试集名称更新名称=eee,其中id=1”代码>到
String sql = "UPDATE test SET name='eee' WHERE id=1";
此查询不正确
String sql=“从测试集名称更新名称=eee,其中id=1”
修改为
String sql=“更新测试集名称='eee',其中id=1”
替换:
String sql = "UPDATE name FROM test SET name=eee WHERE id=1";
在
构造查询的另一个好方法是使用“准备好的语句”-看看oracle教程-
它有助于避免像您的案例中那样的引用问题,并提供更高的顺序性。我记得它提供了一些准备,有助于更快地执行查询。首先,您的语句不是有效的update语句。该公约规定:
update <tableName> set <column> = '<newValue>';
当然,这是可行的,但我不喜欢这种方法。这是非常危险和不安全的。我建议您使用参数化语句,这些语句更安全(请注意)且更具可读性
PreparedStatement用法的简单示例:
String sql = "UPDATE test SET name = ? WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, <nameValue>); // binding value for name column
ps.setInt(2, <idValue>); // binding value for where clause
ps.executeUpdate(); // executes statement
String sql=“更新测试集名称=?其中id=?”;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,);//名称列的绑定值
ps.setInt(2,);//where子句的约束值
ps.executeUpdate();//执行语句
我想提及准备好的报表的几个主要优点:
- 它们是预编译的,SQL语句的数据库端缓存
以加快总体执行速度和重用同一SQL的能力
语句的批处理
- 通过内置的转义自动防止SQL注入攻击
引号和其他特殊字符
- 简化SQL中非标准Java对象的设置(日期、时间、,
时间戳、大十进制、Blob等)
@这是一个正确的SQL查询吗?为什么在错误答案上投票这是一个基本语法问题,您可以在中查找。:D我以前测试过它,但它有一个错误!但是现在它的工作())谢谢,所以mutch())我不喜欢这种方法。这是意大利面代码。这很危险,也不太容易让人读懂。“我不会投你反对票,因为这是可行的,但这不是正确的做法。”萨伊蒙完全同意。Asal,你也应该考虑Sajmon在他的回答中建议使用PayReDealScript的方法。正如我在回答中提到的。谢谢你,我正在从图托阅读,然后我使用了那个代码,但是我认为你说的最好的代码,我在PHP中看到了,但是我不知道java的语法,那么我必须学习它。所以谢谢alot@asal不客气。语法不难理解。您需要知道,有占位符(?)将按相同顺序替换为PreparedStatement
类的setXX()
方法中的值。列的索引从1开始。你可以看看。我可以再问一个问题吗?代码中的第一个元素是什么?许多列,例如ps.setString(2,);在我的数据库中,设置为Column 2(表示名称)的属性?如果您有where id=?名称=?
那么它将是setInt(1,id)
和setString(2,name)
第一个?
位于索引1,第二个位于索引2等。如果添加三个?
,则需要设置三个值。
set name = 'value';
String sql = "UPDATE test SET name = ? WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, <nameValue>); // binding value for name column
ps.setInt(2, <idValue>); // binding value for where clause
ps.executeUpdate(); // executes statement