java.sql.SQLSyntaxErrorException
package com.imooc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.imooc.dao.UserDao;
import com.imooc.domain.User;
import com.imooc.utils.JDBCUtils;
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {
}
@Override
public User login(User user) {
// 调用数据库进行判断
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 获得连接
con = JDBCUtils.getConnection();
// 编写SQL?语句
String sql = "select * from user where username= ? and password= ?";
// 预编译
ps = con.prepareStatement(sql);
// 参数设置
ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
// 执行SQl;
rs = ps.executeQuery(sql);
if (rs.next()) {
User u=new User();
u.setUid(rs.getString("uid"));
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
return u;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
JDBCUtils.release(rs, ps, con);
}
return null;
}
}
报错显示
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1b61d76abwb4x5y1358ryn|4036ba82, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1b61d76abwb4x5y1358ryn|4036ba82, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///shop?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {password=******, user=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
java.sql.SQLSyntaxErrorException: 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 '? and password= ?' at line 1
在登录页面输入正确的账户及密码,跳转显示帐户密码错误,控制台显示java.sql.SQLSyntaxErrorException,在sql语句那块报错,但是我把sql语句直接放到数据库并带上参数的查询是可以的。这里是哪里出问题?需要怎么修改呢??
正在回答
同学你好,如下执行的语句中,不需要加参数sql的,如:
修改后的代码:
建议同学修改一下再试试。
祝:学习愉快~
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星