The Artima Developer Community
Sponsored Link

Java Answers Forum
still having Problems

2 replies on 1 page. Most recent reply: Mar 15, 2004 6:34 AM by ben

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

still having Problems Posted: Mar 15, 2004 5:01 AM
Reply to this message Reply
Advertisement
Hello All,

I am still having problems with regrds to my code. When I select the first combo box and select a table Name that begins with 0 it thows an Exception but when I reselect the combo Box to change the table Name it thows the same Exceptiomn again before I can change the table Name.
How can I get my code so that it thows the Exception only once (the first Exception) ? I have posted on this topic before so I have been looking into it, but just can not find the answer.

Please could someone help me

Thankyou

Ben


import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class ComboBoxTable{
static Connection conn2;
static DatabaseMetaData DMD;
static JComboBox group = new JComboBox();
static JComboBox groupList = new JComboBox();
public static void main(String[] args) {

try {

// Load the database driver
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
conn2 = DriverManager.getConnection( "jdbc:odbc:myProject") ;
DMD=conn2 .getMetaData();
ResultSet rs=DMD.getTables(null,null,null,new String[]{"TABLE"});
while(rs.next()) {

group.addItem((String)rs.getString(3));

}
rs.close() ;

} catch (SQLException se ) {
System.out.println( "SQL Exception: " + se ) ;
} catch (Exception e ) {
System.out.println( e ) ;
}

group.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tbl = (String)group.getSelectedItem();
try {
Statement st=conn2.createStatement();
st.execute("SELECT * FROM "+tbl+" WHERE 1=0");
ResultSet rs=st.getResultSet();
ResultSetMetaData md=rs.getMetaData();
groupList.removeAllItems();
for (int i=1;i<=md.getColumnCount();i++) {
groupList.addItem(md.getColumnName(i));
}
st.close() ;

} catch (SQLException se ) {
System.out.println( "SQL Exception: " + se ) ;
} catch (Exception ex ) {
System.out.println( ex ) ;
}
}
});

int j=10;
String[][] data = new String[j][2];
for (int k=0; k<j; k++){
String[] row = {"",""};
data[k] = row;
}

String[] colNames = {"Column A", "Column B"};
JTextField jtf = new JTextField();
JTable tbl = new JTable(data, colNames);
// Create the combo box editor
DefaultCellEditor editor = new DefaultCellEditor(groupList);
// ADDED IN MY ATTEMPT TO HAVE TO COLUMNS WITH COMBOBOXES
DefaultCellEditor editor2 = new DefaultCellEditor(group);
// Assign the editor to the second column
TableColumnModel tcm = tbl.getColumnModel();
tcm.getColumn(0).setCellEditor(editor2);
// ADDED
tcm.getColumn(1).setCellEditor(editor);
// Set column widths
tcm.getColumn(0).setPreferredWidth(200);
tcm.getColumn(1).setPreferredWidth(100);
// Set row heighht
tbl.setRowHeight(20);
tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tbl.setPreferredScrollableViewportSize(tbl.getPreferredSize());
JFrame f = new JFrame("ComboBoxDemo");
f.getContentPane().add(new JScrollPane(tbl), "Center");

f.pack();
f.addWindowListener(new WindowAdapter() {


public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
f.setVisible(true);
}
}


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: still having Problems Posted: Mar 15, 2004 6:03 AM
Reply to this message Reply
What is the exception u are getting??


Is this a valid query??

st.execute("SELECT * FROM "+tbl+" WHERE 1=0");

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: still having Problems Posted: Mar 15, 2004 6:34 AM
Reply to this message Reply
Hi,
I asume this is a valid query.
It runs fine if I don't select a Table that starts with a number.

The Exception I get says: Incorrect Syntax near '0'.

I hope you can see the problem

Thanks

Ben

Flat View: This topic has 2 replies on 1 page
Topic: Hash Previous Topic   Next Topic Topic: Classloader limits?


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us