加载数据库驱动程序!
创建数据库连接!
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();
}
}
sql写错了嘛。
那里不就是应该?