如下代码,出现了异常,求大神解决。说是SQL语句问号出现了异常,看了半天没看出哪里有问题!!!
importjava.sql.*;importjava.util.Random;publicclassSqlName{publicstaticfinalStringurl...
import java.sql.*;
import java.util.Random;
public class SqlName {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String user = "root";
public static final String password = "root";
public static void main(String[] args) {
Connection con= null;
PreparedStatement pst = null;
ResultSet rs = null;
Random ran=new Random();
int i=ran.nextInt(17);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
String s="select name from information where id=? ";//说这里语法有问题
pst=con.prepareStatement(s);
pst.setInt(1, i);
rs = pst.executeQuery(s);
String name = rs.getString("name");
System.out.println(name);
} catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
rs.close();//出错位置
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
如下是异常提示:
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 '?' at line 1 展开
import java.util.Random;
public class SqlName {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String user = "root";
public static final String password = "root";
public static void main(String[] args) {
Connection con= null;
PreparedStatement pst = null;
ResultSet rs = null;
Random ran=new Random();
int i=ran.nextInt(17);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
String s="select name from information where id=? ";//说这里语法有问题
pst=con.prepareStatement(s);
pst.setInt(1, i);
rs = pst.executeQuery(s);
String name = rs.getString("name");
System.out.println(name);
} catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
rs.close();//出错位置
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
如下是异常提示:
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 '?' at line 1 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询