找不到方法 prepareStatement(query) |找不到符号
The method prepareStatement(query) is not found | Symbol not found
The error reads Cannot find Symbol
You can see the connection to the class
我一直收到错误消息,当我使用 getconnection.prepareStatement(query) 时,我什至无法在自动完成中找到它,它甚至不存在。我也试过 My_CNX.getConnection().prepareStatement(query);
我的连接代码class
package login_register_design;
import com.mysql.cj.jdbc.MysqlDataSource;
import com.sun.jdi.connect.spi.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author narai
*/
public class My_CNX {
private static String servername = "localhost";
private static String username = "root";
private static String dbname = "users_db";
private static Integer portnumber = 3306;
private static String password = "";
public static Connection getConnection()
{
Connection cnx = null;
MysqlDataSource datasource = new MysqlDataSource();
datasource.setServerName(servername);
datasource.setUser(username);
datasource.setPassword(password);
datasource.setDatabaseName(dbname);
datasource.setPortNumber(portnumber);
try {
cnx = (Connection) datasource.getConnection();
} catch (SQLException ex) {
Logger.getLogger(" Get Connection -> " + My_CNX.class.getName()).log(Level.SEVERE, null, ex);
}
return cnx;
}
/**
*
* @param query
* @return
*/
public static PreparedStatement prepareStatement(String query) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
我的 JformButton 的代码!
private void jButton_LoginActionPerformed(java.awt.event.ActionEvent evt) {
// PreparedStatement st;
ResultSet rs;
// get the username & password
String username = jTextField_Username.getText();
String password = String.valueOf(jPasswordField.getPassword());
// System.out.println("The password is " + password);
//create a select query to check if the username and the password exist in the database
String query = "SELECT * FROM `users' WHERE `Email` = ? AND `Password` = ?";
// System.out.println(My_CNX.getConnection());
// st = My_CNX.getConnection();
Connection cnx = null;
PreparedStatement cnx1;
// st = (PreparedStatement) My_CNX.getConnection();
PreparedStatement st;
try {
st= My_CNX.getConnection().prepareStatement(query);
st.setString(1, username);
st.setString(2, password);
rs = st.executeQuery();
if(rs.next())
{
// show a new form
Dashboard form = new Dashboard();
form.setVisible(true);
form.pack();
form.setLocationRelativeTo(null);
// close the current form(login form)
this.dispose();
}else{
// error message
JOptionPane.showMessageDialog(null, "Invalid Username / Password","Login Error",2);
}
}
catch (SQLException ex) {
java.util.logging.Logger.getLogger(Login_Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
我为按钮导入 class
import com.sun.jdi.connect.spi.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.IOException;
经过一些帮助,我设法解决了错误。但是现在当我点击按钮时,我收到了这个错误!
圣哉!现在没有错误!太感谢了!当我点击登录按钮时仍然出现错误。
May 26, 2021 3:52:02 PM login_register_design.Login_Form jButton_LoginActionPerformed
SEVERE: null
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ClientPreparedStatement.checkBounds(ClientPreparedStatement.java:1373)
at com.mysql.cj.jdbc.ClientPreparedStatement.getCoreParameterIndex(ClientPreparedStatement.java:1386)
at com.mysql.cj.jdbc.ClientPreparedStatement.setString(ClientPreparedStatement.java:1753)
at login_register_design.Login_Form.jButton_LoginActionPerformed(Login_Form.java:253)
at login_register_design.Login_Form.actionPerformed(Login_Form.java:131)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
您正在导入:
com.sun.jdi.connect.spi.Connection;
应该是:
java.sql.Connection
The error reads Cannot find Symbol
You can see the connection to the class
我一直收到错误消息,当我使用 getconnection.prepareStatement(query) 时,我什至无法在自动完成中找到它,它甚至不存在。我也试过 My_CNX.getConnection().prepareStatement(query);
我的连接代码class
package login_register_design;
import com.mysql.cj.jdbc.MysqlDataSource;
import com.sun.jdi.connect.spi.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author narai
*/
public class My_CNX {
private static String servername = "localhost";
private static String username = "root";
private static String dbname = "users_db";
private static Integer portnumber = 3306;
private static String password = "";
public static Connection getConnection()
{
Connection cnx = null;
MysqlDataSource datasource = new MysqlDataSource();
datasource.setServerName(servername);
datasource.setUser(username);
datasource.setPassword(password);
datasource.setDatabaseName(dbname);
datasource.setPortNumber(portnumber);
try {
cnx = (Connection) datasource.getConnection();
} catch (SQLException ex) {
Logger.getLogger(" Get Connection -> " + My_CNX.class.getName()).log(Level.SEVERE, null, ex);
}
return cnx;
}
/**
*
* @param query
* @return
*/
public static PreparedStatement prepareStatement(String query) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
我的 JformButton 的代码!
private void jButton_LoginActionPerformed(java.awt.event.ActionEvent evt) {
// PreparedStatement st;
ResultSet rs;
// get the username & password
String username = jTextField_Username.getText();
String password = String.valueOf(jPasswordField.getPassword());
// System.out.println("The password is " + password);
//create a select query to check if the username and the password exist in the database
String query = "SELECT * FROM `users' WHERE `Email` = ? AND `Password` = ?";
// System.out.println(My_CNX.getConnection());
// st = My_CNX.getConnection();
Connection cnx = null;
PreparedStatement cnx1;
// st = (PreparedStatement) My_CNX.getConnection();
PreparedStatement st;
try {
st= My_CNX.getConnection().prepareStatement(query);
st.setString(1, username);
st.setString(2, password);
rs = st.executeQuery();
if(rs.next())
{
// show a new form
Dashboard form = new Dashboard();
form.setVisible(true);
form.pack();
form.setLocationRelativeTo(null);
// close the current form(login form)
this.dispose();
}else{
// error message
JOptionPane.showMessageDialog(null, "Invalid Username / Password","Login Error",2);
}
}
catch (SQLException ex) {
java.util.logging.Logger.getLogger(Login_Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
我为按钮导入 class
import com.sun.jdi.connect.spi.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.IOException;
经过一些帮助,我设法解决了错误。但是现在当我点击按钮时,我收到了这个错误!
圣哉!现在没有错误!太感谢了!当我点击登录按钮时仍然出现错误。
May 26, 2021 3:52:02 PM login_register_design.Login_Form jButton_LoginActionPerformed
SEVERE: null
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ClientPreparedStatement.checkBounds(ClientPreparedStatement.java:1373)
at com.mysql.cj.jdbc.ClientPreparedStatement.getCoreParameterIndex(ClientPreparedStatement.java:1386)
at com.mysql.cj.jdbc.ClientPreparedStatement.setString(ClientPreparedStatement.java:1753)
at login_register_design.Login_Form.jButton_LoginActionPerformed(Login_Form.java:253)
at login_register_design.Login_Form.actionPerformed(Login_Form.java:131)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
您正在导入: com.sun.jdi.connect.spi.Connection;
应该是: java.sql.Connection