在我的Java程序中,我无法运行Addplaylist.java
文件。它打开很好......但是我能够add a playlist
我需要数据库中每个member
的名字!
我已经添加了一些System.out.println("")'s
来检查我在文件中的位置,并且由于某种原因,我只会将其转到addPlaylist() GUI
。我已经尝试调试它并找到错误但我找不到它!
这个文件是从main GUI
调用的 - 我知道数据库中有值,并且连接正常,因为我在另一个文件中使用过它。
这是我的addPlaylist.java
:
package GUI;
import DBconnection.connection;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author james
*/
public class addPlaylist extends JFrame implements ActionListener{
private Container mainCon = this.getContentPane();
private JPanel formPanel = new JPanel();
private JPanel paidPanel = new JPanel();
/*ALL USER INPUT FIELDS*************************/
//Buttons
private static JButton submitPlaylist = new JButton("Add Playlist");
/*TEXT FIELDS BOXES *******************************/
private JLabel lblPlaylistName = new JLabel("Playlist Name: ");
private JTextField txtPlaylistName = new JTextField("", 15);
/*TEXT FIELDS BOXES *******************************/
/*COMBO BOXES *******************************/
private JLabel lblNames = new JLabel();
private JComboBox comNames = new JComboBox();
/*COMBO BOXES *******************************/
/*DECLARED VARIABLES ****************************/
// private String membersData = "";
/*DECLARED VARIABLES ****************************/
public addPlaylist(){
super("Add a Playlist");
this.setBounds(400, 500, 315,400);
mainCon.add(formPanel);
/** BEGINNING OF FORM ************/
formPanel.add(lblPlaylistName);
formPanel.add(txtPlaylistName);
//Combobox (Names)
formPanel.add(lblNames);
formPanel.add(comNames);
//comNames.addActionListener(this);
//txtPlaylistName.addActionListener(this);
System.out.println("In add playlist");
}
public void actionPerformed(ActionEvent action) {
System.out.println("In Action Performed");
/**ADD DATABASE CONNECTIONS*****************/
/****** Set the default code - needed for SQL statements *************/
Connection conDB = null;
Statement stmt = null;
ResultSet membersData = null;
/** GET MYSQL DETAILs ---------**/
String url = connection.geturl();
String username = connection.getUsername();
String password = connection.getPassword();
try{
System.out.println("In Try Statement");
/****** DEFAULT MYSQL DRIVERS **************************/
//load the MYSQL driver
Class.forName(connection.getDriver());
conDB = DriverManager.getConnection(url, username, password);
//create statement
stmt = conDB.createStatement();
/****** DEFAULT MYSQL DRIVERS **************************/
String sqlSelect = "SELECT membId, name, memberType FROM members";
membersData = stmt.executeQuery(sqlSelect);
System.out.println("After executed");
while(membersData.next()){
// comNames.addItem(membersData.getString("name"));
System.out.println("In while loop");
String membId = membersData.getString("membId");
System.out.println(membId);
// System.out.println(membersData.getString("name"));
// System.out.println(membersData.getString("memberType"));
}
//membersData.close();
}
catch(SQLException er){
System.out.println("Error was: " + er);
} catch (ClassNotFoundException ex) {
System.out.println("SECOND ERROR: " + ex);
}
}
}
答案 0 :(得分:0)
因此,要解决此问题,我所做的就是将Combo box
所需的所有代码都包含在名为Database connections and result sets etc...
的函数中的showCombo()
中。然后我在showCombo()
方法中调用addPlaylist() { }
。
像这样:
public void showCombo(){
/****** Set the default code - needed for SQL statements *************/
Connection conDB = null;
Statement stmt = null;
ResultSet membersData = null;
/** GET MYSQL DETAILs ---------**/
String url = connection.geturl();
String username = connection.getUsername();
String password = connection.getPassword();
try{
System.out.println("In Try Statement");
/****** DEFAULT MYSQL DRIVERS **************************/
//load the MYSQL driver
Class.forName(connection.getDriver());
conDB = DriverManager.getConnection(url, username, password);
//create statement
stmt = conDB.createStatement();
/****** DEFAULT MYSQL DRIVERS **************************/
String sqlSelect = "SELECT membId, name, memberType FROM members";
membersData = stmt.executeQuery(sqlSelect);
System.out.println("After executed");
while(membersData.next()){
comNames.addItem(membersData.getString("name"));
// System.out.println("In while loop");
// String membId = membersData.getString("membId");
// System.out.println(membId);
// System.out.println(membersData.getString("name"));
// System.out.println(membersData.getString("memberType"));
}
//membersData.close();
}
catch(SQLException er){
System.out.println("Error was: " + er);
} catch (ClassNotFoundException ex) {
System.out.println("SECOND ERROR: " + ex);
}
}