gpt4 book ai didi

java - 从 MySql 数据库中检索值到 Java

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:52 25 4
gpt4 key购买 nike

我使用以下代码从 MySql 数据库中检索数据。 GContnStr 是我调用的连接数据库的连接方法。但是,当我使用这段代码时,出现以下异常。我该如何解决这个问题?

package designstudent;

import java.sql.*;
import java.awt.event.*;
import java.beans.EventHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import java.util.ArrayList.*;
import java.util.Vector;
import javax.swing.*;
public class combobutton extends javax.swing.JFrame {

Statement TmpStmnt=null;
ResultSet TmpDetlRs=null;
GContnStr GCS=new GContnStr();
String GStrSql=null;


private String PFldname="StudClass";
private String PTName="studentmaster";



enum options{
DBTABLE,
DBQryDef
}

String PoptnStr;

options val = options.valueOf(PoptnStr);

public combobutton() throws SQLException {
combo(PoptnStr, TmpStmnt, TmpDetlRs, GStrSql, PFldname, PTName);
initComponents();
}
public void combo(String PoptnStr,Statement TmpStmnt,ResultSet TmpDetlRs,String StrSql,String PFldName,String PTName) throws SQLException
{
cbx1.removeAllItems();
cbx2.removeAllItems();
cbx3.removeAllItems();
String DBTABLE = null;
try{
switch(val)
{
case DBTABLE:
case DBQryDef:
if(PoptnStr==DBTABLE)
{
GCS.GContnStr();
GStrSql="select '"+ PFldName+"' from '"+PTName+"'";
TmpDetlRs=TmpStmnt.executeQuery(GStrSql);
while(TmpDetlRs.next())
{
String TmpOb1=TmpDetlRs.getString("StudClass");
System.out.println(TmpOb1);
}

}
}
}
catch(Exception e){
System.out.println(e);

}
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new combobutton().setVisible(true);

} catch (SQLException ex) {
Logger.getLogger(combobutton.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}

// Variables declaration - do not modify
private javax.swing.JComboBox cbx1;
private javax.swing.JComboBox cbx2;
private javax.swing.JComboBox cbx3;
// End of variables declaration

}

错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Name is null
at java.lang.Enum.valueOf(Enum.java:236)
at designstudent.combobutton$options.valueOf(combobutton.java:30)
at designstudent.combobutton.<init>(combobutton.java:37)
at designstudent.combobutton$1.run(combobutton.java:122)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:660)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 1 second)

最佳答案

此问题与数据库无关。你的问题就在这里:

 String PoptnStr; 
options val = options.valueOf(PoptnStr);

您创建了初始化为 nullString,然后立即使用它,因此您编写了 options.valueOf(null);

此外,按照惯例,类名应以大写字母开头(即 options 应为 Options),实例变量名应为驼峰式(即 PoptnStr 应该是 poptnStr).

关于java - 从 MySql 数据库中检索值到 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8723374/

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