Issue
The error reads Cannot find Symbol
You can see the connection to the class
I keep getting the error that when I use getconnection.prepareStatement(query) I can't even find it in autocomplete , it doesn't even exist. I've also tried My_CNX.getConnection().prepareStatement(query);
The Code for my connection 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.
}
}
The code for my 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);
}
}
My Imports for the Button 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;
After some help, I've managed to solve the error.. But now when I click on the button I'm getting this error thrown at me!
Holy! There's no error now! Thank you so much! I'm still getting an error when I click the login button tho.
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$6.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$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.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$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.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)
Solution
You're importing: com.sun.jdi.connect.spi.Connection;
It should be: java.sql.Connection
Answered By - Guillaume
Answer Checked By - Pedro (JavaFixing Volunteer)