gpt4 book ai didi

java - 如何判断java中的错误

转载 作者:行者123 更新时间:2023-12-02 00:51:23 24 4
gpt4 key购买 nike

我只是一个java初学者。您对如何确定错误有任何提示吗?我正在尝试连接到 mysql derby 数据库。我不知道如何确定错误,没有红线,但是当我尝试运行程序时会出现一个消息框。我想做的就是显示数据库中的第一条记录。我得到的只是输出中的内容:

E:\Users\users.netbeans\6.8\var\cache\executor-snippets\run.xml:45:

package Employees;

import java.sql.Statement;

import javax.swing.JOptionPane;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
*
* @author Nrew
*/
public class Students extends javax.swing.JFrame {

Connection con;
Statement stmt;
ResultSet rs;


/** Creates new form Students */
public Students() {
initComponents();

DoConnect();

}

public void DoConnect(){
try {
String host= "jdbc:derby://localhost:1527/YURA";
String uname = "bart";
String pword = "12345";
con = DriverManager.getConnection(host, uname, pword);

stmt = con.createStatement( );
String SQL = "SELECT * FROM APP.XROSS";
rs = stmt.executeQuery(SQL);
rs.next();

rs.next( );
int ids = rs.getInt("IDNUM");
String idz = Integer.toString(ids);
String fname = rs.getString("FNAME");
String lname = rs.getString("LNAME");
String course = rs.getString("COURSE");
String skul = rs.getString("SCHOOL");
String gen = rs.getString("GENDER");

TextIDNUM.setText(idz);
TextFNAME.setText(fname);
TextLNAME.setText(lname);
textCOURSE.setText(course);
textSCHOOL.setText(skul);
textGENDER.setText(gen);







}
catch (SQLException err) {
JOptionPane.showMessageDialog(Students.this, err.getMessage());



}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

TextIDNUM = new javax.swing.JTextField();
TextFNAME = new javax.swing.JTextField();
TextLNAME = new javax.swing.JTextField();
textCOURSE = new javax.swing.JTextField();
textSCHOOL = new javax.swing.JTextField();
textGENDER = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(116, 116, 116)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(textGENDER, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textSCHOOL, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textCOURSE, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TextLNAME, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TextFNAME, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TextIDNUM, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
.addContainerGap(243, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(TextIDNUM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(TextFNAME, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(TextLNAME, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(textCOURSE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textSCHOOL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textGENDER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(67, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Students().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JTextField TextFNAME;
private javax.swing.JTextField TextIDNUM;
private javax.swing.JTextField TextLNAME;
private javax.swing.JTextField textCOURSE;
private javax.swing.JTextField textGENDER;
private javax.swing.JTextField textSCHOOL;
// End of variables declaration

}

最佳答案

像这样打印stackTrace:

catch (SQLException err) {
err.printStackTrace(); // <-- add this

JOptionPane.showMessageDialog(Students.this, err.getMessage());
}

关于java - 如何判断java中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2942895/

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