matthew weeks

Greenhorn
+ Follow
since Feb 23, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by matthew weeks

I have a JTable which displays the results of queries carried out upon a database, with the results being displayed in a JTable. This works fine, until the JTable has to display records which require the vertical scroll bar to show them all.
When this happens the first lot of records are displayed, trying to scroll down cannot be done. If the window is minimised then made active again, it goes back to the previous set of results being displayed.
Help me please , the code that Ive used for the table is below -
public void displayResultSet( ResultSet rs )
throws SQLException
{
// position to first record
boolean moreRecords = rs.next();

System.out.println("Do records exist 567");
// If there are no records, display a message
if ( ! moreRecords )
{

JOptionPane.showMessageDialog( this,
"No bookings for this Room or Staff member" );
System.out.println("Dialog box");
return;
}

Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("548 Obtain column heads");

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );

// get row data

System.out.println("551 do get Rows");
do {
rows.addElement( getNextRow( rs, rsmd ) );

System.out.println("552 Get next row");
} while ( rs.next() );
// display table with ResultSet contents
table = new JTable( rows, columnHeads );
System.out.println("558 new JTable");

scroller = new JScrollPane(table);
scroller.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setAutoscrolls(true);
scroller.setBounds(3, 3, 590, 150);
scroller.setLocation(30,325);

System.out.println("568 scroller ");
table.repaint();
getContentPane().add(scroller);

//jScrollPane2.add(table);
getContentPane().add(
scroller, BorderLayout.CENTER );

table.repaint();
validate();
System.out.println("576 validate");

}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
txtaOutput.append("\n " + sqlex.toString());
}
}
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{

Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
System.out.println("599");
break;
case Types.INTEGER:
System.out.println("602");
currentRow.addElement(
new Long( rs.getLong( i ) ) );

break;
default:
txtaOutput.append("\nType was: " +
rsmd.getColumnTypeName( i ) );
System.out.println("605 ");

}

return currentRow;
}
20 years ago
I know this is possible, but I'm not sure how. I was wondering how you go about updating records found in a JTable. Any code (preferred) or guidance would be most appreciated.
I seem to have a problem refreshing the data within my JTable. I know the code is being run through.
I have a JTable which is populated from a selection in a combo box (this is a result set of a query using JDBC), on first click within the combo box the table displays the correct data. In no data is found for the JTable to display a dialog box is prompted to the user (from this i know that the code is generally working and being run through) which states that no records were found. Subsequent clicks either bring up the dialog box saying that "no records were found" (which are correct) or selecting a value within the combo box that should display records only displays the data from before and never changes.
So basically the data within the JTable doesnt refresh. From searching the forum, ive found similar problems which mention something like "fireTableDataChanged" which i cannot find within java.
The code that Im using to populate the JTable is below:-
public void tableDisplayQueryResults()
{

try
{

String query = "SELECT Courses.CourseCode, LessonName.LessonName, Staff.StaffSName, LessonTimes.LessonTimes, LessonTimesEnd.LessonTime, Lessons.LessonDateStart, Lessons.LessonDateEnd " +
"FROM (LessonTimesEnd RIGHT JOIN (LessonTimes RIGHT JOIN ((Courses RIGHT JOIN (Rooms RIGHT JOIN Lessons ON Rooms.RoomName = Lessons.RoomNumber) ON Courses.CourseCode = Lessons.CourseID) LEFT JOIN Staff ON Lessons.StaffID = Staff.StaffID) ON LessonTimes.LessonTimeID = Lessons.LessonTimeStart) ON LessonTimesEnd.LessonTimeID = Lessons.LessonTimeEnd) LEFT JOIN LessonName ON Lessons.LessonNameID = LessonName.LessonNameID " +
"WHERE (((Rooms.RoomName)='" + convertedRoomNumberVector + "'))" +
"ORDER BY LessonTimes.LessonTimes, LessonTimesEnd.LessonTime, Lessons.LessonDateStart, Lessons.LessonDateEnd";
System.out.println("508 String query");

statementTable = connectionTable.createStatement();
resultSet = statementTable.executeQuery( query );
displayResultSet( resultSet );
statementTable.close();

}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
txtaOutput.append("\n" + sqlex.toString());
}

}
//Table to display the bookings for the selected room
public void displayResultSet( ResultSet rs )
throws SQLException
{
// position to first record
boolean moreRecords = rs.next();

System.out.println("527 rs.next");
// If there are no records, display a message
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"No bookings for this room" );
System.out.println("Dialog box");
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );

System.out.println("548 Obtain column heads");

// get row data

System.out.println("551 do get Rows");
do {
rows.addElement( getNextRow( rs, rsmd ) );
System.out.println("552 Get next row");
} while ( rs.next() );
// display table with ResultSet contents
table = new JTable( rows, columnHeads );
System.out.println("558 new JTable");
scroller = new JScrollPane(table);
scroller.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setAutoscrolls(true);
scroller.setBounds(3, 3, 590, 150);
scroller.setLocation(30,250);



System.out.println("568 scroller ");

getContentPane().add(scroller);

//jScrollPane2.add(table);
getContentPane().add(
scroller, BorderLayout.CENTER );

validate();
System.out.println("576 validate");
//setVisible true
// getContentPane().setVisible(true);
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
txtaOutput.append("\n " + sqlex.toString());
}
}
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{

Vector currentRow = new Vector();
System.out.println("593 Add rows");
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
System.out.println("599");
break;
case Types.INTEGER:
System.out.println("602");
currentRow.addElement(
new Long( rs.getLong( i ) ) );

break;
default:
txtaOutput.append("\nType was: " +
rsmd.getColumnTypeName( i ) );
System.out.println("605 ");
}

return currentRow;

}

please help me
20 years ago
I did mean frame, sorry. Ive done it now anyway
20 years ago
I'm trying to do a produce a Computerised Room timetabling system for my IT
department at College using Java and JDBC. So far I've managed to add/delete/edit records for information on Rooms, Staff Members and Courses.
I'm now trying to add lessons for courses, here is where my problem lies.
The IT department want the possibility that some rooms within the department to be doubled booked, as a class generally contains about 30
students and some rooms have the capacity of 100 computers. So a room
could easily contain 2/3 classes at the same time. How this can be
acheived using Java? Making sure that staff members and times for lessons
arent also doubled booked aswell, e.g. a lessons time (time it starts and
finish) doesnt overlap with another lesson.
Hope this is clear (its probably not), Any help with this would be most
appreciated.
Im fairly new to java jdbc, and im sure this is a basic problem. Im having trouble trying to get 4 different queries (basic select from where) to display their results in 4 different combo boxes. I can get each of jcombobox to display its relevant query one at a time, but not all 4 together. When i try to execute the form, I get the following SQL exception error: Invalid Handle, on the following line of code:-

resultSetLessonDay = statementLessonDay.executeQuery(queryLessonDay);
I'm not sure if this is right, but ive created a connection, statement, resultSet and Vector for each query.
Anyone willing to help out a poor student. The code for this class is below:-
/*
* frmLessons.java
*
* Created on 27 February 2004
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;
public class frmLessons extends javax.swing.JFrame {

Connection connectionQueryRooms, connectionCourseCode, connectionStaffMember, connectionLessonDay;
String url;
Vector vtrRoomNumber = new Vector();
Vector vtrCourseCode = new Vector();
Vector vtrStaffMember = new Vector();
Vector vtrLessonDay = new Vector();

/** Creates new form frmLessons */
public frmLessons() {
initComponents();
}

//Initialize the form
public void initComponents() {

lblTitle = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
txtaOutput = new javax.swing.JTextArea();
jSeparator1 = new javax.swing.JSeparator();
butMainMenu = new javax.swing.JButton();
lblInstructions = new javax.swing.JLabel();

cmbRoomNumber = new javax.swing.JComboBox(vtrRoomNumber);
cmbCourseCode = new javax.swing.JComboBox(vtrCourseCode);
cmbStaffMember = new javax.swing.JComboBox(vtrStaffMember);
cmbLessonDay = new javax.swing.JComboBox(vtrLessonDay);
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

//CourseCode combo box properties
cmbCourseCode.setAutoscrolls(true);
cmbCourseCode.setMaximumRowCount(6);
cmbCourseCode.setToolTipText("Select a Course Code to make the Lesson for");
cmbCourseCode.setSize(60,30);
cmbCourseCode.setLocation(30,120);
getContentPane().add(cmbCourseCode);

//RoomNumber combo box properties
cmbRoomNumber.setAutoscrolls(true);
cmbRoomNumber.setMaximumRowCount(6);
cmbRoomNumber.setToolTipText("Select Room Number for a Lesson to be held in");
cmbRoomNumber.setSize(60,30);
cmbRoomNumber.setLocation(130,120);
getContentPane().add(cmbRoomNumber);

//StaffMember combo box properties
cmbStaffMember.setAutoscrolls(true);
cmbStaffMember.setMaximumRowCount(6);
cmbStaffMember.setToolTipText("Select a Staff Member for the lesson");
cmbStaffMember.setSize(60,30);
cmbStaffMember.setLocation(190,120);
getContentPane().add(cmbStaffMember);

//LessonDay combo box properties
cmbLessonDay.setAutoscrolls(true);
cmbLessonDay.setMaximumRowCount(6);
cmbLessonDay.setToolTipText("Select a Day for a Lesson");
cmbLessonDay.setSize(60,30);
cmbLessonDay.setLocation(250,120);
getContentPane().add(cmbLessonDay);

//Title label
lblTitle.setFont(new java.awt.Font("Dialog", 1, 18));
lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTitle.setText("Lessons");
getContentPane().add(lblTitle);
lblTitle.setBounds(0, 20, 564, 24);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
txtaOutput.setLineWrap(true);
txtaOutput.setWrapStyleWord(true);
jScrollPane1.setViewportView(txtaOutput);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(10, 450, 220, 60);
getContentPane().add(jSeparator1);
jSeparator1.setBounds(0, 430, 560, 10);
butMainMenu.setText("Main Menu");
butMainMenu.setToolTipText("Go back to main menu, will lose any unsaved changes");
getContentPane().add(butMainMenu);
butMainMenu.setBounds(450, 460, 100, 26);
lblInstructions.setFont(new java.awt.Font("Dialog", 0, 12));
lblInstructions.setText("Use the following components, in order to create a valid Lesson booking for a course:-");
getContentPane().add(lblInstructions);
lblInstructions.setBounds(10, 50, 480, 16);

try
{
url = "jdbc dbc atabase";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connectionQueryRooms = DriverManager.getConnection(url);
connectionCourseCode = DriverManager.getConnection(url);
connectionStaffMember = DriverManager.getConnection(url);
connectionLessonDay = DriverManager.getConnection(url);


txtaOutput.append( "Connection successful MATT\n"); //println connection successful (\n could be new line
}
catch(ClassNotFoundException cnfex ) //Catches/details all class not found exceptions
//process ClassNotFoundExceptions/database here
{
cnfex.printStackTrace();
txtaOutput.append( "Connection successfulM\n" + cnfex.toString());
}
catch (SQLException sqlex) //Catches/details all SQLs not found exceptions
//process SQL exceptions here
{
sqlex.printStackTrace();
txtaOutput.append("Connection successful\n" + sqlex.toString());

}
catch (Exception ex)
{
//process remaining exceptions here
ex.printStackTrace();
txtaOutput.append( ex.toString());
}



cmbQueryRooms();
cmbQueryCourseCode();
cmbQueryStaffMember();
cmbQueryLessonDay();

pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new java.awt.Dimension(564, 556));
setLocation((screenSize.width-564)/2,(screenSize.height-556)/2);
}
public void cmbQueryRooms()
{
try
{
String queryRoomNumber;

String url = "jdbc dbc atabase";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connectionQueryRooms = DriverManager.getConnection(url);

txtaOutput.append("Connection to for queries sucessful\n");

Statement statementQueryRooms = connectionQueryRooms.createStatement(); // public Statement statement, statement1;
queryRoomNumber = "SELECT * FROM Rooms ORDER BY Rooms.RoomNumber ";
txtaOutput.append("\nSending query Room Numbers for comboBoxes: " + connectionQueryRooms.nativeSQL(queryRoomNumber) + "\n");

statementQueryRooms.close();


ResultSet resultSetRoomNumber;
resultSetRoomNumber = statementQueryRooms.executeQuery(queryRoomNumber);

txtaOutput.append("\nQuery sucessful\n");

while(resultSetRoomNumber.next())
{
//States the same of the column value for the vector
vtrRoomNumber.add(resultSetRoomNumber.getString("RoomName"));
}
}
catch (ClassNotFoundException cnfex )
{
cnfex.printStackTrace();
txtaOutput.append(" Connection unsuccessful\n" + cnfex.toString());
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
txtaOutput.append(" Connection unsucessful\n" + sqlex.toString());
}
catch (Exception ex)
{
ex.printStackTrace();
txtaOutput.append(ex.toString());
}
}
public void cmbQueryCourseCode()
{
try
{
String queryCourseCode;

//statement = connection.createStatement();
url = "jdbc dbc atabase";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connectionCourseCode = DriverManager.getConnection(url);

txtaOutput.append("Connection to for queries sucessful\n");

Statement statementCourseCode = connectionCourseCode.createStatement();
queryCourseCode = "SELECT * FROM Courses ORDER BY Courses.CourseCode ";

txtaOutput.append("\nSending queries for comboBox Course Code" + connectionCourseCode.nativeSQL(queryCourseCode)+ "\n");

statementCourseCode.close();
ResultSet resultSetCourseCode;
resultSetCourseCode = statementCourseCode.executeQuery(queryCourseCode);

txtaOutput.append("\nQuery sucessful\n");

while(resultSetCourseCode.next()) //&& resultSetLessonDay.next()))
{
//States the same of the column value for the vector
vtrCourseCode.add(resultSetCourseCode.getString("CourseCode"));
}}
catch (ClassNotFoundException cnfex )
{
cnfex.printStackTrace();
txtaOutput.append(" Connection unsuccessful\n" + cnfex.toString());
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
txtaOutput.append(" Connection unsucessful\n" + sqlex.toString());
}
catch (Exception ex)
{
ex.printStackTrace();
txtaOutput.append(ex.toString());
}
}
public void cmbQueryStaffMember()
{
try
{
String queryStaffMember;

//statement = connection.createStatement();
String url = "jdbc dbc atabase";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connectionStaffMember = DriverManager.getConnection(url);

txtaOutput.append("Connection to for queries sucessful\n");

Statement statementStaffMember = connectionStaffMember.createStatement(); // public Statement statement, statement1;
queryStaffMember = "SELECT * FROM Staff ORDER BY Staff.StaffSName ";

txtaOutput.append("\nSending queries for comboBox Staff Members" + connectionStaffMember.nativeSQL(queryStaffMember)+ "\n");

statementStaffMember.close();

ResultSet resultSetStaffMember;
resultSetStaffMember = statementStaffMember.executeQuery(queryStaffMember);

txtaOutput.append("\nQuery sucessful\n");

while(resultSetStaffMember.next()) //&& resultSetLessonDay.next()))
{
//States the same of the column value for the vector
vtrStaffMember.add(resultSetStaffMember.getString("StaffSName"));
}
}
catch (ClassNotFoundException cnfex )
{
cnfex.printStackTrace();
txtaOutput.append(" Connection unsuccessful\n" + cnfex.toString());
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
txtaOutput.append(" Connection unsucessful\n" + sqlex.toString());
}
catch (Exception ex)
{
ex.printStackTrace();
txtaOutput.append(ex.toString());
}
}
public void cmbQueryLessonDay()
{
try
{

String queryLessonDay;

//statement = connection.createStatement();
String url = "jdbc dbc atabase";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connectionLessonDay = DriverManager.getConnection(url);

txtaOutput.append("Connection to for queries sucessful\n");

Statement statementLessonDay = connectionLessonDay.createStatement();
queryLessonDay = "SELECT * FROM DayOfWeek ";

txtaOutput.append("\nSending queries for comboBox Days " + connectionLessonDay.nativeSQL(queryLessonDay )+ "\n");
statementLessonDay.close();

ResultSet resultSetLessonDay;
resultSetLessonDay = statementLessonDay.executeQuery(queryLessonDay);
txtaOutput.append("\nQuery sucessful\n");

while(resultSetLessonDay.next()) //&& resultSetLessonDay.next()))
{
//States the same of the column value for the vector
vtrLessonDay.add(resultSetLessonDay.getString("DayName"));
}
}
catch (ClassNotFoundException cnfex )
{
cnfex.printStackTrace();
txtaOutput.append(" Connection unsuccessful\n" + cnfex.toString());
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
txtaOutput.append(" Connection unsucessful\n" + sqlex.toString());
}
catch (Exception ex)
{
ex.printStackTrace();
txtaOutput.append(ex.toString());
}

}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
new frmLessons().show();
}

// Variables declaration
public javax.swing.JLabel lblInstructions;
public javax.swing.JSeparator jSeparator1;
public javax.swing.JScrollPane jScrollPane1;
public javax.swing.JButton butMainMenu;
public javax.swing.JTextArea txtaOutput;
public javax.swing.JLabel lblTitle;
public javax.swing.JComboBox cmbRoomNumber;
public javax.swing.JComboBox cmbCourseCode;
public javax.swing.JComboBox cmbStaffMember;
public javax.swing.JComboBox cmbLessonDay;

// End of variables declaration

}
I've completely forgotten the syntax for how you open up a form from a button? Obviously its on the actionPerformed of the button.
I know you change something (possibly in the Strings[] args bit) in the form you want to open to something like showForm().
20 years ago
How do you find a record on a form based upon the value selected in a JComboBox.
Just tried out what you said, and I get an error when compiling the code:-
setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
value.setText(box.getSelectedItem());
where value = textfield and box = jcombobox
This wouldn't be that the values in the combo box are obtained from a vector. If so how do I overcome this problem
20 years ago
When you select a row within a JComboBox how do you get that row to be printed in a textfield. Am I right in thinking that you use the ItemSelected method?
Any help with this would be greatly appreciated.
20 years ago
Thanks for your help, ive final got it working.
Its the JDBC thats confusing me, im not the worlds expert on it, as you can tell. In the resultSet.getString, do you state the column names or what?
I dont suppose you've got a full example/code that you could give us to show how it works.
Im trying to use either combo boxes or lists menus (in Swing) to allow me to select a specific column values from a table using JDBC and Access in order for me to add a record to the database.
I would greatly appreciate any help on this matter, as im going insane because of this.