Skip navigation links

MySQL Forums :: Connector/JDBC and Java :: Populating Swing JList using SQL Database


Advanced Search

Populating Swing JList using SQL Database
Posted by: fwqf qfwwq ()
Date: May 28, 2011 09:58PM

Sup peeps, im trying to to populate a JList using the entries in a table in a database i created.

package networks;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.sql.*;

public class Contestantlist extends javax.swing.JFrame {

/** Creates new form contestantList */
public Contestantlist() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

try
{
// Load driver's class, inilialize, register with DriverManager
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("Unable to load driver class");
return;
}

ResultSet rs = null;

try
{

//
// Call DriverManager's methods (all are static)
//

// To print log on sysout
//DriverManager.setLogStream(System.out); //JDBC 1.x driver
// DriverManager.setLogWriter(new PrintWriter(System.out) );

// Connect to database. DriverManager loads each registered driver
// in turn until one can handle the database URL format
//Connection con = DriverManager.getConnection(
// "jdbc:oracle:thin:@host.domain.com:1521:db_name"
// ,"scott","tiger");
// Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
// Get the DatabaseMetaData object to display database info
// DatabaseMetaData dmd = con.getMetaData();
// Statement stmt = con.createStatement();
// String sql;

// sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
// rs = stmt.executeQuery(sql); //throws SQLExecption if fails
// printResultSet(rs);

DriverManager.setLogWriter(new PrintWriter(System.out) );

Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
// Get the DatabaseMetaData object to display database info
DatabaseMetaData dmd = con.getMetaData();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
String sql;
Statement stmt = con.createStatement();
sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
rs = stmt.executeQuery(sql); //throws SQLExecption if fails

ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();*

// Display data, fetching until end of the result set
// Calling next moves to first or next row and returns true if success
while(rs.next() )
{
// Each rs after next() contains next rows data
for(final int i=1; i<=numCols; i++)
{
if(i > 1) System.out.print(",");
// Almost all SQL types can be cast to a string by JDBC
// System.out.print(rs.getString(i));
theList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { rs.getString(i) };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return rs.getString(i); }
});

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
System.out.println("");
}


}
catch(Exception f)
{

}

titleLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
// theList.setVisibleRowCount(6);
theList = new javax.swing.JList();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

titleLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
titleLabel.setText("The Scrupulous Contestants!");



/* theList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 3", "Item 4", "Item 5", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings; }
});*/
jScrollPane1.setViewportView(theList);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(titleLabel))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(71, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
);

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



/* private static void printResultSet(ResultSet rs) throws SQLException
{
DriverManager.setLogWriter(new PrintWriter(System.out) );

Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
// Get the DatabaseMetaData object to display database info
DatabaseMetaData dmd = con.getMetaData();

String sql;
Statement stmt = con.createStatement();
sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
rs = stmt.executeQuery(sql); //throws SQLExecption if fails

ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();

// Display data, fetching until end of the result set
// Calling next moves to first or next row and returns true if success
while(rs.next() )
{
// Each rs after next() contains next rows data
for(int i=1; i<=numCols; i++)
{
if(i > 1) System.out.print(",");
// Almost all SQL types can be cast to a string by JDBC
System.out.print(rs.getString(i));
}
System.out.println("");
}
}*/

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Contestantlist().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JList theList;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel titleLabel;
// End of variables declaration
}



I can tell that im not using something correctly. Can you guys just point out how i should proceed through this? I encapsulated the piece of code that has got me confused.

Options: ReplyQuote


Subject Written By Posted
Populating Swing JList using SQL Database fwqf qfwwq 05/28/2011 09:58PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.