//...public class Test_1{//.... public static void main(String args[]) throws Exception{
//...
PreparedStatement pStmt = null; String sql = "SELECT id, name, password, age, birthday FROM user"
+ "WHERE name LIKE ?";
String keyWord = "a";
pStmt = con.prepareStatement(sql); pStmt.setString(1, "%" + keyWord + "%");
//...
}
}
这个代码报错:Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'LIKE '%a%'' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)
at Test_1.main(Test_1.java:27)
应该是SQL语句的问题,应该如何改呢??? 还有 SQL 的 字符串拼接 有什么简单的学习资料吗?? 就是很多引号的那个
like后面的?要加引号吧 -> '?'
数据库连接没有问题加上不对的, 会提示没有参数 错误信息:Exception in thread "main" java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553)
at Test_1.main(Test_1.java:26)
把1改为0试试,我也不懂
把1改成0就可以了!
还是报错 :
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'LIKE '\'%a%\''' at line 1"\'%" + keyword + "%\'"这样也错的
恩 这个好像我知道 但是我试过这几种1: pStmt.setString(1, "'%" + keyWord + "%'");2: pStmt.setString(1, "\'%" + keyWord + "%\'");3: String keyWord = "'%a%'";4: String keyWord = "\'%a%\'";5: "..." + "WHERE name LIKE '?'";都有报错 这是我的程序(MySQL : 5.5.17) :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;public class Test_1{ public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/lb";
public static final String DBNAME = "root";
public static final String DBPASSWORD = "admin"; public static void main(String args[]) throws Exception{
Class.forName(DBDRIVER);
Connection con = null;
PreparedStatement pStmt = null;
ResultSet rs = null;
String sql = "SELECT id, name, password, age, birthday FROM user"
+ "WHERE name LIKE ?";
String keyWord = "a";
con = DriverManager.getConnection(DBURL, DBNAME, DBPASSWORD);
pStmt = con.prepareStatement(sql);
pStmt.setString(1, "%" + keyWord + "%");
rs = pStmt.executeQuery();
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int age = rs.getInt(4);
java.util.Date birthday = rs.getDate(5);
System.out.print("ID:" + id + " ");
System.out.print("NAME:" + name + " ");
System.out.print("PASSWORD:" + password + " ");
System.out.print("AGE" + age + " ");
System.out.println("BIRTHDAY:" + birthday + " ");
System.out.println("---------------------------------");
}
pStmt.close();
con.close();
}
}
请问如何改正, SQL语句的拼接有什么窍门不?? 有没有这方面的学习实例??
LZ结贴吧