gpt4 book ai didi

java - 尝试将 java 连接到 mysql 时出现 MySQLSyntaxErrorException

转载 作者:行者123 更新时间:2023-11-29 18:16:27 24 4
gpt4 key购买 nike

我不擅长java,并且仍在尝试如何通过jdbc驱动程序将java连接到mysql(xampp),有人知道为什么我不断收到错误吗?

    public class connect {



public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{


ResultSet rs = null;
PreparedStatement pst = null;
String hostName = "localhost";
String dbPort = "3306";
String databaseName = "Libraryusers";
String dbUser = "root";
String dbPassword = "";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://"+hostName+":"
+ dbPort+"/"+databaseName+"?"+"user="+dbUser+"&password=" + dbPassword);
System.out.print("Database is connected !");

String sql = "select * from login where username = ? and password = ?";

pst = conn.prepareStatement(sql);
pst.setString(1,"rod1");
pst.setString(2,"rod");
rs = pst.executeQuery(sql);
if (rs.next())
{
System.out.println("username and password is correct");
}
else
{
System.out.println("invalid username and password");
}





}

}

这是控制台结果

run:
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 MariaDB server version for the right syntax to use near '? and password = ?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2483)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2441)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1381)
at login.connect.main(connect.java:44)
Database is connected
!C:\Users\rodchris\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

我已经创建了数据库并插入了几个数据,其中一个是“用户名= rod1,密码= rod”

最佳答案

您通过在 executeQuery(sql) 处再次指定 SQL 脚本来取消参数...

尝试这样的事情:

public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException,
SQLException
{
final String hostName = "localhost";
final String dbPort = "3306";
final String databaseName = "Libraryusers";
final String dbUser = "root";
final String dbPassword = "";

// this is not needed anymore, but many still use it...
Class.forName("com.mysql.jdbc.Driver").newInstance();

try (final Connection conn = DriverManager.getConnection("jdbc:mysql://" + hostName + ":" +
dbPort + "/" + databaseName + "?user=" + dbUser + "&password=" +
dbPassword))
{
System.out.print("Database is connected !");

final String sql = "SELECT * FROM `login` WHERE `username` = ? AND `password` = ?";

try ( final PreparedStatement pst = conn.prepareStatement(sql))
{
pst.setString(1,"rod1");
pst.setString(2,"rod");
try (final ResultSet rs = pst.executeQuery())
{
if (rs.next())
{
System.out.println("username and password is correct");
}
else
{
System.out.println("invalid username and password");
}
}
}
}
}

关于java - 尝试将 java 连接到 mysql 时出现 MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com