英文:
java.sql.PreparedStatement cannot be converted to com.mysql.jdbc.PreparedStatement
问题
package demo;
import java.awt.HeadlessException;
import java.sql.Statement;
import java.sql.Connection; // Import the correct Connection class
import java.sql.PreparedStatement; // Import the correct PreparedStatement class
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import java.sql.SQLException;
public class NewJFrame extends javax.swing.JFrame {
Connection con;
PreparedStatement pst;
ResultSet rs;
public NewJFrame() {
initComponents();
}
// ... (Rest of the code remains the same)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String uname=jTextField1.getText();
String pword=jPasswordField1.getText();
String option=jComboBox1.getSelectedItem().toString();
if(uname.equals("")||pword.equals("")||option.equals("")){
JOptionPane.showMessageDialog(rootPane,"Some fields are Empty!!","Error",1);}
else{
try{
con=DriverManager.getConnection("jdbc:mysql://your_database_url_here", "username", "password"); // Provide your database URL, username, and password
pst=con.prepareStatement("select * from `login` where username=? and password=?"); // Corrected typo 'passwor' to 'password'
pst.setString(1, uname); // Corrected typo 'unmae' to 'uname'
pst.setString(2, pword);
rs=pst.executeQuery();
if(rs.next()){
String s1=rs.getString("option");
if(option.equalsIgnoreCase("Admin")&&s1.equalsIgnoreCase("admin"))
{
AdminPage ad=new AdminPage(uname);
ad.setVisible(true);
setVisible(false);
}
if(option.equalsIgnoreCase("User")&&s1.equalsIgnoreCase("user")) // Corrected 'admin' to 'user'
{
UserPage ad=new UserPage(uname);
ad.setVisible(true);
setVisible(false);
}
}
else{
JOptionPane.showMessageDialog(rootPane, "Username or password not matched","Login Error", 1);
}
}
catch(Exception ex){ // Corrected 'Excepton' to 'Exception'
System.out.println(""+ex);}
}
}
// ... (Rest of the code remains the same)
}
Please note:
- You need to replace
"jdbc:mysql://your_database_url_here", "username", "password"
with your actual database URL, username, and password. - I've corrected the typos in the code, like the misspelled variable names, and replaced
Connections.getConnection()
withDriverManager.getConnection()
. Also, I've corrected the usage of "option" as "user" instead of "admin" when checking for the user role. - If you are still encountering errors after these corrections, ensure that your database connection details are accurate and that you have the MySQL JDBC driver in your project's classpath.
英文:
I was trying to make a GUI application using mysql database connection which is getting error.
Here is my code.
package demo;
import java.awt.HeadlessException;
import java.sql.Statement;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import java.sql.SQLException;
public class NewJFrame extends javax.swing.JFrame {
Connection con;
PreparedStatement pst;
ResultSet rs;
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox<>();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jPasswordField1 = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setBackground(new java.awt.Color(51, 204, 0));
jLabel1.setFont(new java.awt.Font("Times New Roman", 2, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("LOGIN PAGE");
jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
jLabel2.setText("Username");
jLabel3.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
jLabel3.setText("Password");
jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
jLabel4.setText("Select User");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select", "Admin", "User" }));
jButton1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton1.setText("LOGIN");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton2.setText("CANCEL");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(54, 54, 54)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(70, 70, 70)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
.addComponent(jPasswordField1))
.addContainerGap(149, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(jComboBox1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String uname=jTextField1.getText();
String pword=jPasswordField1.getText();
String option=jComboBox1.getSelectedItem().toString();
if(uname.equals("")||pword.equals("")||option.equals("")){
JOptionPane.showMessageDialog(rootPane,"Some fiels are Empty!!","Error",1);}
else{
try{
con=Connections.getConnection();
pst=con.prepareStatement("select * from `login` where username=? and passwor=?");
pst.setString(1, unmae);
pst.setString(2, pword);
rs=pst.executeQuery();
if(rs.next()){
String s1=rs.getString("option");
if(option.equalsIgnoreCase("Admin")&&s1.equalsIgnoreCase("admin"))
{
AdminPage ad=new AdminPage(uname);
ad.setVisible(true);
setVisible(false);
}
if(option.equalsIgnoreCase("User")&&s1.equalsIgnoreCase("admin"))
{
UserPage ad=new UserPage(uname);
ad.setVisible(true);
setVisible(false);
}
}
else{
JOptionPane.showMessageDialog(rootPane, "Username orr passswoord not mached","Login Error", 1);
}
}
catch(Excepton ex){
System.out.println(""+ex);}
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
}
At lines
con=Connections.getConnection();
pst=con.prepareStatement("select * from `login` where username=? and passwor=?");
pst.setString(1, unmae);
it is getting error.
It says that cannot find symbol variable Connections.
incompatible types:java.sql.PreparedStatement cannot be converted to com.mysql.jdbc.PreparedStatement
Please help me.
答案1
得分: 1
在您的导入中存在一个问题,您需要从 java.sql
而不是从 com.mysql.jdbc
导入 Connection
、PreparedStatement
和 ResultSet
,所以请使用:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
而不是:
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
英文:
There are a problem in your imports, you have to import Connection
, PreparedStatement
, and ResultSet
from java.sql
and not from com.mysql.jdbc
, so use :
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
instead of this:
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
答案2
得分: 1
在 Connections.getConnection() 的位置,应该使用 DriverManager 来初始化连接。
类似这样:
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + "://" +
this.serverName +
":" + this.portNumber + "/",
connectionProps);
来源:https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html#db_connection_url
英文:
in the place of Connections.getConnection(), should use DriverManager to initialize the connection.
Something like
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + "://" +
this.serverName +
":" + this.portNumber + "/",
connectionProps);
https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html#db_connection_url
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论