我在基于swing的项目中工作。我想在用户登录成功时打开一个新窗口但是当我点击登录按钮异常时出现" org.net-bean.ExitSecurityException:非法尝试早退出#34;我也检查我的记录使用打印输出一切正常,但我不知道如何解决这个问题。这是我的代码。
import java.awt.Component;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import java.sql.*;
import javax.swing.*;
import org.openide.util.Exceptions;
class CoreTopComponent extends TopComponent {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
private String password;
private String pwd;
private Component jPasswordField1;
private Component jButton1;
//private Object text_password;
public CoreTopComponent() {
initComponents();
conn = mysqlconnect.ConnecrDb();
}
private void ClickActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object password = text_password.getText();
String sql = "Select * from assistant where pass =?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, text_password.getText());
rs = pst.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, "You are login successfully");
//setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.openide.LifecycleManager.getDefault();
Login s = new Login();
s.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "You are not login successfully");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
private void text_passwordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object password = text_password;
}
public static void main(String args[]) {
CoreTopComponent core = new CoreTopComponent();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CoreTopComponent().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JToggleButton Click;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JOptionPane jOptionPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPasswordField text_password;
}
我的mysql连接代码在这里
package org.Core.Art;
import java.sql.*;
import javax.swing.*;
public class mysqlconnect {
Connection conn = null;
public static Connection ConnecrDb() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/auto_lube", "root", "mehar");
JOptionPane.showMessageDialog(null, "Connection established");
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
我访问了很多网站,但我无法解决。有人分享任何链接或指导我,谢谢。
答案 0 :(得分:1)
恕我直言,您仍然遇到this answer中所述的同样问题:您正在使用sun.jdbc.odbc.JdbcOdbcDriver
连接到MySQL数据库。此驱动程序旨在使用JDBC-ODBC桥接来获取连接,但这不是这种情况。更不用说在JDK 8中删除了Removal of JDBC ODBC bridge in java 8。
正如我所说,你不应该使用DriverManager.getConnection()而是使用DataSource.getConnection()。因此,在您的MySqlConnect
类中(注意Java Code Convention类和方法名称)进行此更改:
public class MySqlConnect {
public static Connection connectToDB() {
try {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setDatabaseName("auto_lube");
dataSource.setPortNumber(3306);
dataSource.setUser("root");
dataSource.setPassword("mehar");
Connection conn = dataSource.getConnection();
JOptionPane.showMessageDialog(null, "Connection established"); // better log this message
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e); // better log the exception
return null;
}
}
}
当然,您必须在项目中加入正确的MySQL JDBC Connector并停止使用sun.jdbc.odbc.JdbcOdbcDriver