首页 新闻 搜索 专区 学院

语法没错却报错??????

0
悬赏园豆:5 [待解决问题]

加载数据库驱动程序!
创建数据库连接!
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 '问号' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
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:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2627)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1556)
at beans.LoginBean.searchRecordsByElement(LoginBean.java:43)
at beans.LoginBean.main(LoginBean.java:77)

、、、、、、、、、、、、、、、、、、、、、、

public class LoginBean {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static Connection conn = null;

static {// 负责加载数据库驱动
try {
System.out.println("加载数据库驱动程序!");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("在加载数据库驱动程序时抛出异常,内容如下:");
e.printStackTrace();
}
}
static{
try {
System.out.println("创建数据库连接!");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
System.out.println("在创建数据库连接时抛出异常,内容如下:");
e.printStackTrace();
}
}

public void searchRecordsByElement(String n,String number){

//int num=Integer.parseInt(number);

try {
String sql = "select * from student where name= ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1,n);
ResultSet result = pst.executeQuery( sql);

if(result.next()){
System.out.print(result.getString(1)+" ");
if(number==result.getString(2))
{
System.out.println("you are right!");
}
else{
System.out.println("you are error!");
}
}
else{
System.out.println("####################");
}
pst.close();
conn.close();

} catch (SQLException e) {
e.printStackTrace();
}

}

邗影的主页 邗影 | 初学一级 | 园豆:73
提问于:2014-04-04 21:43
< >
分享
所有回答(1)
0

sql写错了嘛。

angelshelter | 园豆:9676 (大侠五级) | 2014-04-04 22:32

那里不就是应该?

支持(0) 反对(0) 邗影 | 园豆:73 (初学一级) | 2014-04-05 09:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册