Swing-jdbc

we are developing a swing applicaiton.it is connecting to an oracle 10gr2 database.all database calls are written seperately in a class and conncetions are closed in finaly block.the problem is that on invoking the application few times in a row, the following sql exception is happening
java.lang.NullPointerException
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
The Connection descriptor used by the client was:
172.16.18.192:1521:oncodms
is it due to some network problem or database server related problem?? or some problem with the code?? ive seen connection pooling in j2ee containers ,is it possible to implement something like that in swing application??

Hi friends.
I'm newer in Java, and i'm searching for 3er party
components (comercial or free), that connects swing
with jdbc (for example, one tablemodel that connects
with jdbc for read and write).
Thank you.Please see http://forum.java.sun.com/thread.jspa?threadID=5123381
Edit: Hey Stupid! That thread is yours. Cripes how stupid can you get....

Similar Messages

  • Crystal Report for Eclipse - Java SWING - JDBC Examples?

    <p>Good day! I am just a newbie in Java Programming and I am wondering if there is an example application in SWING-JDBC w/reports that uses Crystal reports w/ Eclipse IDE. As what I have noticed, all of the examples I have seen here are all in jsp and I havent seen anything for swing-jdbc only. I want to try using crystal reports in java. </p><p>&#160;</p><p>Do you guys have some examples (even a very simple one) that uses crystal reports with Eclipse and even with a simple database like Access? Any sample, tutorial downloads with this topic? hope you guys can help. thank you very much and God bless.</p>

    Crystal Reports doesn't have the same niece tooling for desktop/swing applications that it does for JSP/Web applications. That said you can still create reports with CR4Eclipse and use them in a Swing applications. Try out these samples that I posted. They are all desktop/swing applications. These will work with Eclipse.
    [http://diamond.businessobjects.com/node/707 | /node/707]
    [http://diamond.businessobjects.com/node/335 | /node/335]
    Hopefully this is helpful.
    Rob Horne
    Rob's blog - http://diamond.businessobjects.com/blog/10</p>

  • Swing - Jdbc Components

    Hi friends.
    I'm newer in Java, and i'm searching for 3er party components (comercial or free), that connects swing with jdbc (for example, one tablemodel that connects with jdbc for read and write).
    Thank you.

    Hi friends.
    I'm newer in Java, and i'm searching for 3er party
    components (comercial or free), that connects swing
    with jdbc (for example, one tablemodel that connects
    with jdbc for read and write).
    Thank you.Please see http://forum.java.sun.com/thread.jspa?threadID=5123381
    Edit: Hey Stupid! That thread is yours. Cripes how stupid can you get....

  • Implicit vs explicit close of resultsets and statements?

    Hi friends.I am a newbie Java Developer..Okay Here goes
    I have just made a LAN based Java application using Swing,JDBC with backend as MS-Access..The backend is on a shared network drive..
    The application is distributed as jar files on the LAN PCs..
    Everywhere I have connected to the database I have just closed the connection explicitly like this
    con.close();
    I do not close the associated resultset and statement explicitly
    The specification says associated statements and resultsets close when you close
    the connection,even if you don't explicitly close them
    Also I am not using connection pool..its simple basic connection using DriverManager
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbcdbcSN name";
    String user = "";
    String pw = "";
    con = DriverManager.getConnection(url, user, pw);
    Statement stmt = con.createStatement();
    String select = "" ;
    ResultSet rows = stmt.executeQuery(select);
    On the net everyone says to explicitly close everything..but I did not know that
    earlier..
    If specification says everything closes on
    closing connection why do ppl insist on closing everything explicitly..?
    Or is this driver dependent..don't the drivers go through the specification..
    My driver is the Sun JDBC ODBC bridge.....
    I found this method DriverManager.setLogwriter()
    It prints out a trace of all JDBC operations..
    So I ran a sample program with this method included...
    I redirected output to a log file..
    In that program I just explicitly close the connection without closing the
    statements and resultsets explicitly...
    After running the program and seeing the log I saw that the statements
    and resultsets are closed implicitly If I just close the connection explicitly..
    I am putting the log file and the code..
    Have a look at the end of the log file..
    Code
    import java.sql.;
    import java.io.;
    class gc4test
    public static void main(String args[])
    Connection con = null;
    try
    FileWriter fwTrace = new FileWriter("c:\\log.txt");
    PrintWriter pwTrace= new PrintWriter(fwTrace);
    DriverManager.setLogWriter(pwTrace);
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:pravahcon";
    String user = "admin";
    String pw = "ash123";
    con = DriverManager.getConnection(url, user, pw);
    Statement stmt = con.createStatement();
    Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    Statement stmt2 = con.createStatement();
    Statement stmt3 = con.createStatement();
    Statement stmt4 = con.createStatement();
    Statement stmt5 = con.createStatement();
    Statement stmt6 = con.createStatement();
    Statement stmt7 = con.createStatement();
    String select = "SELECT * FROM Users" ;
    ResultSet rows = stmt.executeQuery(select);
    ResultSet rows1 = stmt1.executeQuery(select);
    while(rows.next())
    con.close();
    catch (ClassNotFoundException f)
    System.out.println(f.getMessage());
    System.exit(0);
    catch (SQLException g)
    System.out.println(g.getMessage());
    System.exit(0);
    catch (Exception e)
    System.out.println(e.getMessage());
    System.exit(0);
    End of Log File
    Setting statement option (SQLSetStmtAttr), hStmt=50275112, fOption=25
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    Fetching (SQLFetch), hStmt=50274224
    End of result set (SQL_NO_DATA)
    *Connection.close
    8 Statement(s) to close
    *Statement.close
    Free statement (SQLFreeStmt), hStmt=50281544, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@2e7263
    *Statement.close
    Free statement (SQLFreeStmt), hStmt=50277224, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@1bf216a
    *Statement.close
    *ResultSet.close
    *ResultSet has been closed
    Free statement (SQLFreeStmt), hStmt=50274224, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@156ee8e
    *Statement.close
    Free statement (SQLFreeStmt), hStmt=50280464, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@c20e24
    *Statement.close
    Free statement (SQLFreeStmt), hStmt=50278304, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@12ac982
    *Statement.close
    *ResultSet.close
    *ResultSet has been closed
    Free statement (SQLFreeStmt), hStmt=50275112, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@e0e1c6
    *Statement.close
    Free statement (SQLFreeStmt), hStmt=50276144, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@6ca1c
    *Statement.close
    Free statement (SQLFreeStmt), hStmt=50279384, fOption=1
    deregistering Statement sun.jdbc.odbc.JdbcOdbcStatement@1389e4
    Disconnecting (SQLDisconnect), hDbc=50271048
    Closing connection (SQLFreeConnect), hDbc=50271048
    Closing environment (SQLFreeEnv), hEnv=50270880
    So like what these implicitly closed statements and resultsets are different from explicitly closed
    resultsets and statements..?

    Please do not crosspost/doublepost the same question again. It is rude in terms of netiquette.
    Stick to one topic: [http://forums.sun.com/thread.jspa?threadID=5393387&messageID=10745794#10745794].

  • Just closing connection enough?

    Hi friends..This is my first post...I am a newbie Java Developer..Okay Here goes
    I have just made a LAN based Java application using Swing,JDBC with backend as MS-Access..The backend is on a shared network drive..
    The application is distributed as jar files on the LAN PCs..
    Everywhere I have connected to the database I have just closed the connection explicitly like this
    con.close();
    I do not close the associated resultset and statement explicitly
    The specification says associated statements and resultsets close when you close
    the connection,even if you don't explicitly close them
    Also I am not using connection pool..its simple basic connection using DriverManager
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbcdbcSN name";
    String user = "";
    String pw = "";
    con = DriverManager.getConnection(url, user, pw);
    Statement stmt = con.createStatement();
    String select = "" ;
    ResultSet rows = stmt.executeQuery(select);
    Till now no performance problems..over 4K records have been added..
    JVM is not exiting or anything..
    On the net everyone says to explicitly close everything..but I did not know that
    earlier..
    My question is
    Is closing just the connection enough..?
    Is there any way I can check whether statements and resultsets have been implicitly closed?
    Later on will it cause any performance problems or anything...?
    Shud I change my code and add rs.close() and stmt.close() everywhere..?
    If specification says everything closes on
    closing connection why do ppl insist on closing everything explicitly..?
    Or is this driver dependent..don't the drivers go through the specification..
    My driver is the Sun JDBC ODBC bridge.....
    There would be 8-10 users maximum at a time using the application concurrently...
    Any help would be appreciated..Thanks in advance..

    @jschell
    Thanks for your help
    I was running a few more tests..can you please explain the the following code...
    Code
    import java.sql.*;
    import java.io.*;
    class gc5test
    public static void main(String args[])
    Connection con = null;
    long memoryBeforeSelects;
    long memoryAfterSelects;
    long memoryAfterConnectionClose;
    try
    PrintWriter pw= new PrintWriter("c:\\log1.txt");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:pravahcon";
    String user = "admin";
    String pass = "ash123";
    for(int i=1;i<=20;i++)
    pw.println("Connection No"+i);
    memoryBeforeSelects = Runtime.getRuntime().freeMemory();
    pw.println("Free Memory before making connection " + memoryBeforeSelects / 1024 + " kB");
    con = DriverManager.getConnection(url, user, pass);
    Statement stmt = con.createStatement();
    String select = "SELECT * FROM Users" ;
    ResultSet rows = stmt.executeQuery(select);
    memoryAfterSelects = Runtime.getRuntime().freeMemory();
    pw.println("Free Memory after making connection " + memoryAfterSelects / 1024 + " kB");
    rows.close();
    stmt.close();
    con.close();
    memoryAfterConnectionClose = Runtime.getRuntime().freeMemory();
    pw.println("Free Memory after closing connection " + memoryAfterConnectionClose / 1024 + " kB");
    pw.close();
    catch (ClassNotFoundException f)
    System.out.println(f.getMessage());
    System.exit(0);
    catch (SQLException g)
    System.out.println(g.getMessage());
    System.exit(0);
    catch (Exception e)
    System.out.println(e.getMessage());
    System.exit(0);
    Output
    Connection No1
    Free Memory before making connection 4729 kB
    Free Memory after making connection 4675 kB
    Free Memory after closing connection 4675 kB
    Connection No2
    Free Memory before making connection 4675 kB
    Free Memory after making connection 4638 kB
    Free Memory after closing connection 4620 kB
    Connection No3
    Free Memory before making connection 4620 kB
    Free Memory after making connection 4584 kB
    Free Memory after closing connection 4584 kB
    Connection No4
    Free Memory before making connection 4584 kB
    Free Memory after making connection 4548 kB
    Free Memory after closing connection 4529 kB
    Connection No5
    Free Memory before making connection 4529 kB
    Free Memory after making connection 4493 kB
    Free Memory after closing connection 4493 kB
    Connection No6
    Free Memory before making connection 4493 kB
    Free Memory after making connection 4457 kB
    Free Memory after closing connection 4457 kB
    Connection No7
    Free Memory before making connection 4439 kB
    Free Memory after making connection 4403 kB
    Free Memory after closing connection 4403 kB
    Connection No8
    Free Memory before making connection 4403 kB
    Free Memory after making connection 4367 kB
    Free Memory after closing connection 4367 kB
    Connection No9
    Free Memory before making connection 4349 kB
    Free Memory after making connection 4313 kB
    Free Memory after closing connection 4313 kB
    Connection No10
    Free Memory before making connection 4313 kB
    Free Memory after making connection 4277 kB
    Free Memory after closing connection 4277 kB
    Connection No11
    Free Memory before making connection 4259 kB
    Free Memory after making connection 4223 kB
    Free Memory after closing connection 4223 kB
    Connection No12
    Free Memory before making connection 4223 kB
    Free Memory after making connection 4187 kB
    Free Memory after closing connection 4169 kB
    Connection No13
    Free Memory before making connection 4169 kB
    Free Memory after making connection 4817 kB
    Free Memory after closing connection 4800 kB
    Connection No14
    Free Memory before making connection 4800 kB
    Free Memory after making connection 4766 kB
    Free Memory after closing connection 4766 kB
    Connection No15
    Free Memory before making connection 4766 kB
    Free Memory after making connection 4714 kB
    Free Memory after closing connection 4714 kB
    Connection No16
    Free Memory before making connection 4714 kB
    Free Memory after making connection 4678 kB
    Free Memory after closing connection 4678 kB
    Connection No17
    Free Memory before making connection 4678 kB
    Free Memory after making connection 4627 kB
    Free Memory after closing connection 4627 kB
    Connection No18
    Free Memory before making connection 4627 kB
    Free Memory after making connection 4592 kB
    Free Memory after closing connection 4575 kB
    Connection No19
    Free Memory before making connection 4575 kB
    Free Memory after making connection 4540 kB
    Free Memory after closing connection 4540 kB
    Connection No20
    Free Memory before making connection 4540 kB
    Free Memory after making connection 4489 kB
    Free Memory after closing connection 4489 kB
    The memory is not being freed immediately after closing connection explicitly in first 12 iterations..
    In the first 12 iterations memory after making connection and memory after closing
    connection are the same/or very less difference...
    Only in the 13th iteration the memory increases from 4169 to 4817..
    Why is this happening..?
    I am closing everything explicitly..
    Shouldn't the memory be reclaimed immediately..?
    Insted it is happening later on..
    Am I missing something here..?

  • Command line windows

    Hi,
    I am using procedures from for example: ctx_ddl, ctx_doc packages (by my program in Java - swing, jdbc). I have problem with command line windows in Windows XP, because they appear when I execute some procedures. What should I do to hide these windows?
    Regards,
    Paul Krolak

    I just fired up DOS edit and saw the blue screen after years and years... it does have one advantage over (plain unadorned) Notepad though -- indents are carried over to the next line.
    @TP -- I didn't find any sluggishness, and my machine isn't spectacularly fast (P4 2.8 GHz HT with 512+256 mismatched 400 MHz RAM running @ 333 MHz / WinXP SP2).
    db

  • Is it possible to make a code editor in Java?

    I'm new to java and so i'd like to know if it would be possible to make a simple code editor for Mobile Phones with Java.
    The code editor will be for GML.
    I'd like to make it so that the code can be easily edited on a wide range Mobile phones (not necissarily smart phones).
    If this is possible, I would greatly appreaciate any help or advice. Thanks.
    skinnyeddy

    TuringPest wrote:
    Hippolyte wrote:
    skinnyeddy wrote:
    "first list the areas in which you are proficient" - I'm not. I thought it would be easy to make a simple text/code editor in java without any experience. That was a bit of a mistake. Looks like I'll have to learn the hard way :SIMHO, one should woodshed it, work on programming basics and object-oriented programming fundamentals, in Java for at least one year before attempting to write any GUI code. Even day in this forum we see what code looks like when this advice is not followed.my first java code 5 years ago was an applet with a changing 2d function being animated.
    it worked after 2 grueling days and i was so happy because visual feedback was my only motivation.
    so its hard to say. granted, my code was awful for a long time.I'm thinking of the guy last week who had Swing+JDBC code and kept pestering for help on an obvious syntax error (he was invoking a method with no arguments when it was defined to take a String array) and for the life of him, he couldn't see the error or understand the error message. Posters kept telling him to learn the fundamentals first, but he was stubborn and kept demanding someone fix his syntax error...

  • How to automatically generate messages

    hello.
    as part of the ongoing development of my video library system (done using Java, Swing + JDBC) - i must be able to generate an Overdue Reminder message and a Fine message in the form of JOptionPane messages. in order to do this i must be able read the loan information from my video library database so that i can generate such messages. i have pseudocode already made out for this.
    IF ReturnDate is greater than 7th day
    Display OverdueReminder
    IF Product is not returned after OverdueReminder is issued
    Display Fine
    IF Fine is not paid
    Display AccountDisabledNotice
    how can i turn this pseudocode into code?

    yes. what i have to do now is learn how to generate messages.
    thanks for the reply.

  • Java Developers Urgently Required-Guildford UK -up to 45,000 per annum

    Permanent Java Developer urgently required for a leading pre-press work flow software house.
    We need developers with the following skills:
    Java J2EE
    JSF OR Spring OR Struts
    JSP Servlets / Swing
    JDBC SQL
    JDOM / HTML / JavaScript / CSS / XML / XSL / DTD / XPATH
    Desktop: Windows XP
    Eclipse3 / CVS
    Basic UML
    These skills are an advantage.
    C++/Perl/AJAX
    Parking, excellent working hours and an immediate start available for the right candidate.
    Please Contact:
    Kelly Riddell
    01483 300515
    [email protected]

    I think that it is something wrong with kernel and graphic drivers but i'm not able to solve it from the beginning of using of Arch by me.
    I'm not so sure this issue is software-related only because in my case it seems the laptop screen enters power-saving mode (10-min timeout) on its own without any events logged (as shown by dmesg) so my next thought was OK, I'm going to get rid of this issue disabling the screen power-saving timeout in the BIOS and set it to never power down or something like that and from then on managing power mode manually; ie: when I want to power down I will just use systemctl suspend/hibernate which work fine ... problem is: there are no related options in my BIOS for such timeout.
    The only timeouts I have are in /etc/systemd/logind.conf set to 30min (#IdleActionSec) (commented) by default so these are not in effect because the screens goes blank after 10m which is less than the (default) 30m in logind. Maybe setting loggind to 9m (un-commented) to catch the event before the machine-default one ?
    IdleActionSec=9min
    IdleAction=suspend
    Will check it and report back.

  • Crystal Report with Java

    Hi
    I m using NetBeans for Java(Swing+JDBC) development
    I wana call Crystal Report file (.rpt) file from Java
    I found following source on Sun Forums:-
    import java.awt.*;
    import javax.swing.*;
    import com.crystaldecisions.ReportViewer.*;
    // Use these to talk to RAS
    import com.crystaldecisions.sdk.occa.report.application.*;
    import com.crystaldecisions.sdk.occa.report.reportsource.*;
    public class HelloWorldSwing
        private static void createAndShowGUI()
            try
                //Make sure we have nice window decorations.
                JFrame.setDefaultLookAndFeelDecorated(false);
                //Create and set up the window.
                JFrame frame = new JFrame("HelloWorldSwing");
                frame.setTitle( "Testing 1, 2, 3");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ReportViewerBean     viewer = new ReportViewerBean();
                viewer.init( new String[0], null, null, null);
                ReportClientDocument     rpt = new ReportClientDocument();
                // FLui: set the RAS server if you are using RAS
                rpt.setReportAppServer( "localhost" );
                rpt.open( "ras://C:\\Reports\\MyReport.rpt", 0 );
                IReportSource     rptSource = rpt.getReportSource();
                viewer.setReportSource( rptSource );
                frame.getContentPane().add( viewer, BorderLayout.CENTER );
                frame.setSize( 700, 500 );
                frame.setVisible(true);
                viewer.start();
            catch ( Exception exception )
                System.out.println( exception.toString() );
        public static void main(String[] args)
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            //com.crystaldecisions.ReportViewer.ReportViewerFrame.main( args );
            javax.swing.SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
    }Presently I need ReportViewer.jar
    If any one have, please give me at <[email protected]>
    Also guide me how we can pass sql & parameters to Crystal Report

    http://www.google.co.za/search?hl=en&q=%2Bjava+%2B%22crystal+reports%22&meta=
    Gives this as the first result:
    http://www.businessobjects.com/products/reporting/crystalreports/java/default.asp
    You then search the site:
    http://www.businessobjects.com/search/?summary=1&client=www_frontend&proxystylesheet=www_frontend&output=xml_no_dtd&lr=lang_en&oe=utf8&ie=utf8&website=&ct=livewww-all&q=java
    Which leads you to this page:
    http://www.businessobjects.com/products/dev_zone/java/default.asp
    Try STFW, RTFM and implement something, then ask a specific question.

  • Certificates and grids

    Do I need a certificate (and how do I do this) to fill in a grid
    with data from a table using a Java applet?
    null

    Marcelo Sanchez (guest) wrote:
    : Do I need a certificate (and how do I do this) to fill in a
    grid
    : with data from a table using a Java applet?
    As long as the database server and webserver are on the same
    machine - no. Even if they are not, use of the Connection
    manager (a NET 8 component) will allow you to spoof the RDBMS
    connection off to another server while appearing to connect to
    the same one, so you would still not need to sign the applet.
    How you do it depends on your choice of grid controls and how
    you want to access the RDBMS (i.e. using a JBCL type bound
    control, or Swing/JDBC with an unbound grid.
    null

  • SWING GUI blockes when doing a DB action with microsoft nativ JDBC driver

    hi,
    in a bigger project i have the problem that if i perform a DB action the GUI is blocked. It does not matter if i do the DB Action in invokeLater or
    in a SwingWorker or in a new Thread if i call the DB Action and i move another window over my window it is not refreshed.
    normally the GUI shouln�t be blocked if i use invokeLater. maybe it
    has something to do that i use the native DB driver? I am using JDK
    1.4. has anyone ever encounterd the same problem?
    alex
    package swingtest;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    public class GUIProblem extends JDialog {   
    // this method does a simple DB call to generate some load
    public void doMyDBCall()
    try {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Connection con = DriverManager.getConnection ("jdbc:microsoft:sqlserver://abgvdr71:1433;databasename=v52li;","sa","");
    Statement question0 = con.createStatement();
    ResultSet rs0=null;
    rs0 = question0.executeQuery("select * from intercept_24");
    boolean more=rs0.next();
    while (more) {    more=rs0.next();}
    catch (Exception sql)
    { sql.printStackTrace(); }
    public void doItinvokeLater()
    SwingUtilities.invokeLater(new Runnable(){    public void run(){       
    doMyDBCall();     
    public void doItSwingWorker()
    SwingWorker worker = new SwingWorker() {
    public Object construct() {
    System.out.println("worker start");
    doMyDBCall();
    return null;
    worker.start();
    System.out.println("finished .....");
    Object view = (Object) worker.get();
    public GUIProblem (){
    super((JFrame)null,"",true);
    JButton button = new JButton("do it");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event)
    // calls it direct -> GUI blocked
    //doMyDBCall();
    // with invoke later -> GUI blocked
    //doItinvokeLater();
    // with the swingWorker -> GUI also blocks
    doItSwingWorker();     
    getContentPane().add(button);
    setSize(200,200);
    setVisible(true);
    public static void main(String [] args)
    {  new GUIProblem(); }
    }

    The invokeLater runs the Runnable in the event thread, so yes, that will block your GUI. You ALMOST got the SwingWorker right. DB call in the construct() method -- the construct() method executes outside the event thread, the finished() method executes in the event thread. The only problem is, you did a worker.get() method in your event thread (right after worker.start(0, and get() blocks until result is ready from construct() -- so, bingo, you're jammed up waiting in you event thread.

  • Using a swing program with a gui-based jdbc program

    hello.
    my name is james mcfadden and i am a final year computing student at letterkenny IT in ireland. i have 2 programs here that i want to make interact with each other. the first one is a jdbc demo and the second one is a login program that i'll be using with the first one. i was trying to get the 2 programs interacting with each other yesterday but i was unsuccessful. would you be able to look at it as soon as possible? i have to be able to use these programs next week. thank you very much.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.JOptionPane;
    import java.sql.*;
    public class Demo extends JFrame {
    static String url = "jdbc:odbc:VideoLibrary";
    static Statement stmt;
    static Connection con;
         //declare member variables here
         JTextField hostField;
    JTextField queryField;
    QueryTableModel qtm;
    JComboBox comboBox;
    public static void main(String args[]){
         //new LogOn();     
    int choice=-1;
              do{
    choice=getChoice();
    if(choice!=0){
    getSelected(choice);
    }while(choice!=5);
    System.exit(0);
    public static int getChoice(){
    String choice;
    int ch;
    choice = JOptionPane.showInputDialog(null,"1. Maintain product details\n"+"2. Maintain member details\n"+"3. Maintain rental details\n"+"4. View product, member and rental details\n"+"5. Log Off\n\n"+"Enter your choice");
              ch = Integer.parseInt(choice);
    return ch;
    public static void getSelected(int choice){   
              if(choice==1){
    addProducts();
    if(choice==2){
    addMembers();
              if(choice==3){
    addRentals();
              if(choice==4){
              Demo test = new Demo();
    test.setVisible(true);
    public static Connection getConnection(){
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              catch(java.lang.ClassNotFoundException e) {
    System.err.print("ClassNotFoundException: ");
    System.err.println(e.getMessage());
    try {
    con=DriverManager.getConnection(url,"","");
              catch(SQLException ex) {
    System.err.println("SQLException: " + ex.getMessage());
    return con;
    public static void addProducts(){
    Connection con = getConnection();
    String addProduct1, addProduct2, addProduct3, addProduct4, addProduct5, addProduct6, addProduct7, addProduct8, addProduct9, addProduct10, addProduct11, addProduct12, addProduct13, addProduct14, addProduct15, addProduct16, addProduct17, addProduct18, addProduct19, addProduct20, addProduct21, addProduct22, addProduct23, addProduct24, addProduct25, addProduct26, addProduct27, addProduct28, addProduct29, addProduct30, addProduct31, addProduct32, addProduct33, addProduct34, addProduct35, addProduct36, addProduct37, addProduct38, addProduct39, addProduct40, addProduct41, addProduct42, addProduct43, addProduct44, addProduct45, addProduct46, addProduct47, addProduct48, addProduct49, addProduct50, addProduct51, addProduct52, addProduct53, addProduct54, addProduct55, addProduct56, addProduct57, addProduct58, addProduct59, addProduct60, addProduct61, addProduct62, addProduct63, addProduct64, addProduct65, addProduct66, addProduct67, addProduct68, addProduct69, addProduct70, addProduct71, addProduct72, addProduct73, addProduct74, addProduct75, addProduct76, addProduct77, addProduct78, addProduct79, addProduct80, addProduct81, addProduct82, addProduct83, addProduct84, addProduct85, addProduct86, addProduct87, addProduct88, addProduct89, addProduct90, addProduct91, addProduct92, addProduct93;
    addProduct1 = "insert into Product values (110001, 'The Killers - Sams Town', 5.00, 'G', 'CD', 2006)";
    addProduct2 = "insert into Product values (110002, 'Robbie Williams - Rudebox', 5.00, 'G', 'CD', 2006)";
    addProduct3 = "insert into Product values (110003, 'Razorlight - Razorlight', 5.00, 'G', 'CD', 2006)";
    addProduct4 = "insert into Product values (110004, 'My Chemical Romance - The Black Parade', 5.00, 'G', 'CD', 2006)";
    addProduct5 = "insert into Product values (110005, 'Snow Patrol - Eyes Open', 5.00, 'G', 'CD', 2006)";
    addProduct6 = "insert into Product values (110006, 'Scissor Sisters - Ta-Dah!', 5.00, 'G', 'CD', 2006)";
              addProduct7 = "insert into Product values (110007, 'Lovesounds - Justin Timberlake', 5.00, 'G', 'CD', 2006)";
    addProduct8 = "insert into Product values (110008, 'Director - We thrive on big cities', 5.00, 'G', 'CD', 2006)";
    addProduct9 = "insert into Product values (110009, 'Roxette - Roxette hits', 5.00, 'G', 'CD', 2006)";
    addProduct10 = "insert into Product values (110010, 'Pussy Cat Dolls - PCD', 5.00, 'G', 'CD', 2006)";
    addProduct11 = "insert into Product values (110011, 'Evanescence - The Open Door', 5.00, 'G', 'CD', 2006)";
    addProduct12 = "insert into Product values (110012, 'Deacon Blue - The Singles', 5.00, 'G', 'CD', 2006)";
              addProduct13 = "insert into Product values (110013, 'Rod Stewart - The Best of Rod Stewart', 5.00, 'G', 'CD', 2006)";
              addProduct14 = "insert into Product values (110014, 'P Diddy - Press Play', 5.00, 'G', 'CD', 2006)";
    addProduct15 = "insert into Product values (110015, 'James Morrison - Undiscovered', 5.00, 'G', 'CD', 2006)";
    addProduct16 = "insert into Product values (110016, 'BDay - Beyonce', 5.00, 'G', 'CD', 2006)";
    addProduct17 = "insert into Product values (110017, 'John Legend - Once Again', 5.00, 'G', 'CD', 2006)";
              addProduct18 = "insert into Product values (110018, 'Luther Vandross - The Ultimate', 5.00, 'G', 'CD', 2006)";
              addProduct19 = "insert into Product values (110019, 'Fleetwood Mac - Greatest Hits', 5.00, 'G', 'CD', 2006)";
              addProduct20 = "insert into Product values (110020, 'Kooks - Inside In Inside Out', 5.00, 'G', 'CD', 2006)";
              addProduct21 = "insert into Product values (110021, 'Scooter - Mind the Gap', 5.00, 'G', 'CD', 2007)";
              addProduct22 = "insert into Product values (110022, 'Scooter - Whos Got the Last', 5.00, 'G', 'CD', 2007)";
              addProduct23 = "insert into Product values (110023, 'Scooter - Jigga Jigga', 5.00, 'G', 'CD', 2007)";
              addProduct24 = "insert into Product values (110024, 'Dr. Alban - Look Whos Talking', 5.00, 'G', 'CD', 2007)";
              addProduct25 = "insert into Product values (110025, 'Dr. Alban - Hello Afrika', 5.00, 'G', 'CD', 2007)";
              addProduct26 = "insert into Product values (110026, 'Dr. Alban - Born in Africa', 5.00, 'G', 'CD', 2007)";
              addProduct27 = "insert into Product values (110027, 'Bon Jovi - Crossroad', 5.00, 'G', 'CD', 2007)";
              addProduct28 = "insert into Product values (110028, 'Bon Jovi - Have a Nice Day', 5.00, 'G', 'CD', 2007)";
              addProduct29 = "insert into Product values (110029, 'Bon Jovi - These Days', 5.00, 'G', 'CD', 2007)";
              addProduct30 = "insert into Product values (120001, 'Mission Impossible 3', 5.00, '15', 'DVD', 2006)";
              addProduct31 = "insert into Product values (120002, 'The Wind that Shakes the Barley', 5.00, '12', 'DVD', 2006)";
              addProduct32 = "insert into Product values (120003, 'The Break Up', 5.00, '12', 'DVD', 2006)";
              addProduct33 = "insert into Product values (120004, 'The Da Vinci Code', 5.00, '15', 'DVD', 2006)";
              addProduct34 = "insert into Product values (120005, 'Ice Age 2', 5.00, 'G', 'DVD', 2006)";
              addProduct35 = "insert into Product values (120006, 'Just My Luck', 5.00, '12', 'DVD', 2006)";
              addProduct36 = "insert into Product values (120007, 'Stormbreaker', 5.00, '12', 'DVD', 2006)";
              addProduct37 = "insert into Product values (120008, 'X-Men 3', 5.00, 'PG', 'DVD', 2006)";
              addProduct38 = "insert into Product values (120009, 'Lake House', 5.00, '12', 'DVD', 2006)";
              addProduct39 = "insert into Product values (120010, 'Inside Man', 5.00, '15', 'DVD', 2006)";
              addProduct40 = "insert into Product values (120011, 'Ultraviolet', 5.00, '12', 'DVD', 2006)";
              addProduct41 = "insert into Product values (120012, 'Friends with Money', 5.00, '15', 'DVD', 2006)";
              addProduct42 = "insert into Product values (120013, 'Fearless', 5.00, 'PG', 'DVD', 2006)";
              addProduct43 = "insert into Product values (120014, 'When A Stranger Calls', 5.00, '15', 'DVD', 2006)";
              addProduct44 = "insert into Product values (120015, 'Prime', 5.00, '15', 'DVD', 2006)";
              addProduct45 = "insert into Product values (120016, 'Poseidon', 5.00, '12', 'DVD', 2006)";
              addProduct46 = "insert into Product values (120017, 'Tristan & Isolde', 5.00, '12', 'DVD', 2006)";
              addProduct47 = "insert into Product values (120018, 'Little Mermaid', 5.00, '12', 'DVD', 2006)";
              addProduct48 = "insert into Product values (120019, 'The Fast & The Furious 3', 5.00, '12', 'DVD', 2006)";
              addProduct49 = "insert into Product values (120020, 'The Omen', 5.00, '15', 'DVD', 2006)";
              addProduct50 = "insert into Product values (120021, 'Eragon', 5.00, '15', 'DVD', 2007)";
              addProduct51 = "insert into Product values (120022, 'Arthur and the Minimoys', 5.00, '15', 'DVD', 2007)";
              addProduct52 = "insert into Product values (120023, 'The Pursuit of Happyness', 5.00, 'G', 'DVD', 2007)";
              addProduct53 = "insert into Product values (120024, 'Van Wilder 2: The Rise of Taj', 5.00, '15', 'DVD', 2007)";
              addProduct54 = "insert into Product values (120025, 'Blood Diamond', 5.00, '15', 'DVD', 2007)";
              addProduct55 = "insert into Product values (120026, 'Deja Vu', 5.00, '15', 'DVD', 2007)";
              addProduct56 = "insert into Product values (120027, 'For Your Consideration', 5.00, '18', 'DVD', 2007)";
              addProduct57 = "insert into Product values (120028, 'The Fountain', 5.00, 'PG', 'DVD', 2007)";
              addProduct58 = "insert into Product values (120029, 'Bobby', 5.00, '15', 'DVD', 2007)";
              addProduct59 = "insert into Product values (130001, 'Need for Special Carbon', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct60 = "insert into Product values (130002, 'Need for Special Carbon', 5.00, '15', 'Nintendo DS Game', 2006)";
              addProduct61 = "insert into Product values (130003, 'Need for Special Carbon', 5.00, '15', 'PSP Game', 2006)";
              addProduct62 = "insert into Product values (130004, 'Need for Special Carbon', 5.00, '15', 'Xbox 360 Game', 2006)";
              addProduct63 = "insert into Product values (130005, 'EYETOY Play SPORTS', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct64 = "insert into Product values (130006, 'Family Fun With Playstation 2', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct65 = "insert into Product values (130007, 'Smackdown v Raw 07', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct66 = "insert into Product values (130008, 'Smackdown v Raw 07', 5.00, '15', 'PSP Game', 2006)";
              addProduct67 = "insert into Product values (130009, 'Smackdown v Raw 07', 5.00, '15', 'Xbox 360 Game', 2006)";
              addProduct68 = "insert into Product values (130010, 'Sinister Legends', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct69 = "insert into Product values (130011, 'Pro Evolution Soccer 6', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct70 = "insert into Product values (130012, 'Pro Evolution Soccer 6', 5.00, '15', 'PSP Game', 2006)";
              addProduct71 = "insert into Product values (130013, 'Pro Evolution Soccer 6', 5.00, '15', 'Xbox 360 Game', 2006)";
              addProduct72 = "insert into Product values (130014, 'BUZZ Jungle Party', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct73 = "insert into Product values (130015, 'FIFA 07', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct74 = "insert into Product values (130016, 'Killzone Liberation', 5.00, '15', 'PSP Game', 2006)";
              addProduct75 = "insert into Product values (130017, 'Gangs of London', 5.00, '15', 'PSP Game', 2006)";
              addProduct76 = "insert into Product values (130018, 'FIFA 07', 5.00, '15', 'PSP Game', 2006)";
              addProduct77 = "insert into Product values (130019, 'Passport to Amsterdam', 5.00, '15', 'PSP Game', 2006)";
              addProduct78 = "insert into Product values (130020, 'Tiger Woods 07', 5.00, '15', 'Playstation 2 Game', 2006)";
              addProduct79 = "insert into Product values (130021, 'Tiger Woods 07', 4.90, '15', 'PSP Game', 2006)";
              addProduct80 = "insert into Product values (130022, 'Tiger Woods 07', 4.90, '15', 'Xbox 360 Game', 2006)";
              addProduct81 = "insert into Product values (130023, 'Madden NFL 07', 4.90, '15', 'Playstation 2 Game', 2006)";
              addProduct82 = "insert into Product values (130024, 'Madden NFL 07', 4.90, '15', 'Nintendo DS Game', 2006)";
              addProduct83 = "insert into Product values (130025, 'Madden NFL 07', 4.90, '15', 'PSP Game', 2006)";
              addProduct84 = "insert into Product values (130026, 'Madden NFL 07', 4.90, '15', 'Xbox 360 Game', 2006)";
              addProduct85 = "insert into Product values (130027, 'Gears of War', 4.90, '15', 'Xbox 360 Game', 2006)";
              addProduct86 = "insert into Product values (130028, 'Star Trek: Legacy', 4.90, '15', 'Xbox 360 Game', 2006)";
              addProduct87 = "insert into Product values (130029, 'Tekken: Dark Ressurection', 4.90, '15', 'PSP Game', 2006)";
              addProduct88 = "insert into Product values (130030, 'GTA Vice city stories PSP', 4.90, '15', 'PSP Game', 2006)";
              addProduct89 = "insert into Product values (130031, 'Star Trek: Encounters', 5.00, '15', 'Playstation 2 Game', 2007)";
              addProduct90 = "insert into Product values (130032, 'Star Trek: Shattered Universe', 5.00, '15', 'Xbox 360 Game', 2007)";
              addProduct91 = "insert into Product values (130033, 'Star Trek Voyager: Elite Force', 5.00, '15', 'Playstation 2 Game', 2007)";
              addProduct92 = "insert into Product values (130034, 'Battlestar Galactica', 5.00, '15', 'Xbox 360 Game', 2007)";
              addProduct93 = "insert into Product values (130035, 'Battlestar Galactica', 5.00, '15', 'Playstation 2 Game', 2007)";
              try {
    stmt = con.createStatement();
    stmt.executeUpdate(addProduct1);
    stmt.executeUpdate(addProduct2);
    stmt.executeUpdate(addProduct3);
    stmt.executeUpdate(addProduct4);
                   stmt.executeUpdate(addProduct5);
    stmt.executeUpdate(addProduct6);
              stmt.executeUpdate(addProduct7);
    stmt.executeUpdate(addProduct8);
    stmt.executeUpdate(addProduct9);
    stmt.executeUpdate(addProduct10);
    stmt.executeUpdate(addProduct11);
    stmt.executeUpdate(addProduct12);
              stmt.executeUpdate(addProduct13);
              stmt.executeUpdate(addProduct14);
    stmt.executeUpdate(addProduct15);
    stmt.executeUpdate(addProduct16);
    stmt.executeUpdate(addProduct17);
              stmt.executeUpdate(addProduct18);
              stmt.executeUpdate(addProduct19);
              stmt.executeUpdate(addProduct20);
              stmt.executeUpdate(addProduct21);
              stmt.executeUpdate(addProduct22);
              stmt.executeUpdate(addProduct23);
              stmt.executeUpdate(addProduct24);
              stmt.executeUpdate(addProduct25);
              stmt.executeUpdate(addProduct26);
              stmt.executeUpdate(addProduct27);
              stmt.executeUpdate(addProduct28);
              stmt.executeUpdate(addProduct29);
              stmt.executeUpdate(addProduct30);
              stmt.executeUpdate(addProduct31);
              stmt.executeUpdate(addProduct32);
              stmt.executeUpdate(addProduct33);
              stmt.executeUpdate(addProduct34);
              stmt.executeUpdate(addProduct35);
              stmt.executeUpdate(addProduct36);
              stmt.executeUpdate(addProduct37);
              stmt.executeUpdate(addProduct38);
              stmt.executeUpdate(addProduct39);
              stmt.executeUpdate(addProduct40);
              stmt.executeUpdate(addProduct41);
              stmt.executeUpdate(addProduct42);
              stmt.executeUpdate(addProduct43);
              stmt.executeUpdate(addProduct44);
              stmt.executeUpdate(addProduct45);
              stmt.executeUpdate(addProduct46);
              stmt.executeUpdate(addProduct47);
              stmt.executeUpdate(addProduct48);
              stmt.executeUpdate(addProduct49);
              stmt.executeUpdate(addProduct50);
              stmt.executeUpdate(addProduct51);
              stmt.executeUpdate(addProduct52);
              stmt.executeUpdate(addProduct53);
              stmt.executeUpdate(addProduct54);
              stmt.executeUpdate(addProduct55);
              stmt.executeUpdate(addProduct56);
              stmt.executeUpdate(addProduct57);
              stmt.executeUpdate(addProduct58);
              stmt.executeUpdate(addProduct59);
              stmt.executeUpdate(addProduct60);
              stmt.executeUpdate(addProduct61);
              stmt.executeUpdate(addProduct62);
              stmt.executeUpdate(addProduct63);
              stmt.executeUpdate(addProduct64);
              stmt.executeUpdate(addProduct65);
              stmt.executeUpdate(addProduct66);
              stmt.executeUpdate(addProduct67);
              stmt.executeUpdate(addProduct68);
              stmt.executeUpdate(addProduct69);
              stmt.executeUpdate(addProduct70);
              stmt.executeUpdate(addProduct71);
              stmt.executeUpdate(addProduct72);
              stmt.executeUpdate(addProduct73);
              stmt.executeUpdate(addProduct74);
              stmt.executeUpdate(addProduct75);
              stmt.executeUpdate(addProduct76);
              stmt.executeUpdate(addProduct77);
              stmt.executeUpdate(addProduct78);
              stmt.executeUpdate(addProduct79);
              stmt.executeUpdate(addProduct80);
              stmt.executeUpdate(addProduct81);
              stmt.executeUpdate(addProduct82);
              stmt.executeUpdate(addProduct83);
              stmt.executeUpdate(addProduct84);
              stmt.executeUpdate(addProduct85);
              stmt.executeUpdate(addProduct86);
              stmt.executeUpdate(addProduct87);
              stmt.executeUpdate(addProduct88);
              stmt.executeUpdate(addProduct89);
              stmt.executeUpdate(addProduct90);
              stmt.executeUpdate(addProduct91);
              stmt.executeUpdate(addProduct92);
              stmt.executeUpdate(addProduct93);
    stmt.close();
    con.close();
              catch(SQLException ex) {
    System.err.println("SQLException: " + ex.getMessage());
    public static void addMembers(){
    Connection con = getConnection();
    String addMember1, addMember2, addMember3, addMember4, addMember5, addMember6, addMember7, addMember8, addMember9, addMember10;
    addMember1 = "insert into Member values (1234, 'Ann', 'Smyth', 'Upper Killult, Falcarragh, Co. Donegal', '(074)-9135210', '(087)-2030172', #5/11/85#, #5/12/06#)";
    addMember2 = "insert into Member values (2345, 'John', 'Murphy', 'Lower Killult, Falcarragh, Co. Donegal', '(074)-9135211', '(087)-2030173', #4/12/85#, #6/13/06#)";
    addMember3 = "insert into Member values (1324, 'James', 'McFadden', 'Lower Ardsbeg, Gortahork, Co. Donegal', '(074)-9165314', '(087)-2030171', #4/11/85#, #6/14/06#)";
    addMember4 = "insert into Member values (1235, 'Frankie', 'Ferry', 'Ardsmore, Gortahork, Co. Donegal', '(074)-9165325', '(087)-2031234', #6/13/60#, #6/15/06#)";
    addMember5 = "insert into Member values (1236, 'Daniel', 'McKimm', 'Ballyness, Falcarragh, Co. Donegal', '(074)-9135212', '(087)-2030184', #5/14/73#, #6/16/06#)";
    addMember6 = "insert into Member values (2346, 'Stephen', 'Doohan', 'Ballyness, Falcarragh, Co. Donegal', '(074)-9135213', '(087)-2030185', #6/13/85#, #5/13/06#)";
    addMember7 = "insert into Member values (2347, 'James', 'Ferry', 'Meenlaragh, Gortahork, Co.Donegal', '(074)-9165360', '(087)-2031345', #9/12/85#, #5/14/06#)";
    addMember8 = "insert into Member values (2348, 'Liam', 'Cannon', 'Derryconner, Gortahork, Co.Donegal', '(074)-9165324', '(087)-2031456', #4/11/86#, #5/15/06#)";
    addMember9 = "insert into Member values (2401, 'Ciaran', 'Ferry', 'Brinalack, Gweedore, Co.Donegal', '(074)-9176425', '(087)-2030282', #9/12/85#, #5/16/06#)";
    addMember10 = "insert into Member values (2402, 'Ciaran', 'McGee', 'Derrybeg, Gweedore, Co.Donegal', '(074)-9176536', '(087)-2030393', #9/14/85#, #5/18/06#)";
    try {
    stmt = con.createStatement();
    stmt.executeUpdate(addMember1);
    stmt.executeUpdate(addMember2);
    stmt.executeUpdate(addMember3);
                   stmt.executeUpdate(addMember4);
                   stmt.executeUpdate(addMember5);
                   stmt.executeUpdate(addMember6);
                   stmt.executeUpdate(addMember7);
                   stmt.executeUpdate(addMember8);
                   stmt.executeUpdate(addMember9);
                   stmt.executeUpdate(addMember10);
    stmt.close();
    con.close();
              catch(SQLException ex) {
    System.err.println("SQLException: " + ex.getMessage());
         public static void addRentals(){
    Connection con = getConnection();
    String addRental1, addRental2, addRental3, addRental4, addRental5, addRental6, addRental7, addRental8, addRental9, addRental10;
    addRental1 = "insert into Rental values (110001, 'The Killers - Sams Town', 1234, 'Ann', 'Smyth', #9/01/06#, #9/10/06#, 'Yes', 2.00)";
    addRental2 = "insert into Rental values (120001, 'Mission Impossible 3', 2345, 'John', 'Murphy', #9/02/06#, #9/09/06#, 'No', 0.00)";
    addRental3 = "insert into Rental values (130001, 'Need for Special Carbon', 1324, 'James', 'McFadden', #9/03/06#, #9/12/06#, 'Yes', 2.00)";
    addRental4 = "insert into Rental values (110002, 'Robbie Williams - Rudebox', 1235, 'Frankie', 'Ferry', #9/04/06#, #9/11/06#, 'No', 0.00)";
    addRental5 = "insert into Rental values (120015, 'Prime', 1236, 'Daniel', 'McKimm', #9/05/06#, #9/14/06#, 'Yes', 2.00)";
    addRental6 = "insert into Rental values (130015, 'FIFA 07', 2346, 'Stephen', 'Doohan', #9/06/06#, #9/13/06#, 'No', 0.00)";
    addRental7 = "insert into Rental values (110009, 'Roxette - Roxette hits', 2347, 'James', 'Ferry', #9/07/06#, #9/16/06#, 'Yes', 2.00)";
    addRental8 = "insert into Rental values (120003, 'The Break Up', 2348, 'Liam', 'Cannon', #9/08/06#, #9/15/06#, 'No', 0.00)";
    addRental9 = "insert into Rental values (130027, 'Gears of War', 2401, 'Ciaran', 'Ferry', #9/09/06#, #9/18/06#, 'Yes', 2.00)";
    addRental10 = "insert into Rental values (110021, 'Scooter - Mind the Gap', 2402, 'Ciaran', 'McGee', #9/10/06#, #9/17/06#, 'No', 0.00)";
    try {
    stmt = con.createStatement();
    stmt.executeUpdate(addRental1);
    stmt.executeUpdate(addRental2);
    stmt.executeUpdate(addRental3);
                   stmt.executeUpdate(addRental4);
                   stmt.executeUpdate(addRental5);
                   stmt.executeUpdate(addRental6);
                   stmt.executeUpdate(addRental7);
                   stmt.executeUpdate(addRental8);
                   stmt.executeUpdate(addRental9);
                   stmt.executeUpdate(addRental10);
    stmt.close();
    con.close();
              catch(SQLException ex) {
    System.err.println("SQLException: " + ex.getMessage());
    public Demo(){
    super("Demo Test Frame");
    setSize(350, 200);
    comboBox = new JComboBox();
    comboBox.addItem("jdbc:odbc:VideoLibrary");
    qtm = new QueryTableModel();
    JTable table = new JTable(qtm);
    JScrollPane scrollpane = new JScrollPane(table);
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 2));
    p1.add(comboBox);
    p1.add(new JLabel("Enter your query: "));
    p1.add(queryField = new JTextField());
    p1.add(new JLabel("Click here to send: "));
    JButton jb = new JButton("Search");
    jb.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    qtm.setHostURL();
    qtm.setQuery(queryField.getText().trim());
    p1.add(jb);
    getContentPane().add(p1, BorderLayout.NORTH);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
    }//end class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class LogOn extends JPanel{
         private static JTextField username=null;
         private static JPasswordField password=null;
         private static JButton button=null;
    public LogOn(){
              setSize(260,160);
              username=new JTextField(15);
         password=new JPasswordField(15);
              //JPanel pane=new JPanel();
              JLabel usernameLabel=new JLabel("Username: ");
              JLabel passwordLabel=new JLabel("Password: ");
              /*pane.*/add(usernameLabel);
              /*pane.*/add(username);
              /*pane.*/add(passwordLabel);
              /*pane.*/add(password);
              //setContentPane(pane);
              show();
              button=new JButton("Ok");
              add(button);
         button.addActionListener(new ButtonListener());
              JFrame frame=new JFrame("Welcome to Home Entertainment");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(new LogOn());
         frame.pack();
         frame.setVisible(true);
         private class ButtonListener implements ActionListener{
         public void actionPerformed(ActionEvent e){
              try{
                        new Demo();
                   catch(Exception ex){}     
    }

    here's the code (again, but it doesn't look as ugly as it did earlier on).
    i have three programs, only 2 of which can interact with each other. QueryTestTable.java is used with Demo.java to display data in a JTable. the third program is called LogOn.java. i want to use the LogOn.java (username and password for logging on to the system) program in order to be able to use the other 2 programs. i have to be able to use these programs next week. thank you very much.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.JOptionPane;
    import java.sql.*;
    public class Demo extends JFrame {
       static String url = "jdbc:odbc:VideoLibrary";
       static Statement stmt;
       static Connection con;
       //declare member variables here
       JTextField hostField;
       JTextField queryField;
       QueryTableModel qtm;
       JComboBox comboBox;
       public static void main(String args[]){
          //new LogOn();
          int choice=-1;
          do{
             choice=getChoice();
             if(choice!=0){
                getSelected(choice);
          }while(choice!=5);
          System.exit(0);
       public static int getChoice(){
          String choice;
          int ch;
          choice = JOptionPane.showInputDialog(null,"1. Maintain product      
          details\n"+"2. Maintain member details\n"+"3. Maintain rental 
          details\n"+"4. View product, member and rental details\n"+"5. Log 
          Off\n\n"+"Enter your choice");
          ch = Integer.parseInt(choice);
          return ch;
       public static void getSelected(int choice){
          if(choice==1){
             addProducts();
          if(choice==2){
             addMembers();
          if(choice==3){
             addRentals();
          if(choice==4){
             Demo test = new Demo();
             test.setVisible(true);
       public static Connection getConnection(){
          try {
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          catch(java.lang.ClassNotFoundException e) {
             System.err.print("ClassNotFoundException: ");
             System.err.println(e.getMessage());
          try {
             con=DriverManager.getConnection(url,"","");
          catch(SQLException ex) {
             System.err.println("SQLException: " + ex.getMessage());
          return con;
       public static void addProducts(){
          Connection con = getConnection();
          String addProduct1, addProduct2;
          addProduct1 = "insert into Product values (110001, 'The Killers -  
          Sams Town', 5.00, 'G', 'CD', 2006)";
          addProduct2 = "insert into Product values (110002, 'Robbie
          Williams - Rudebox', 5.00, 'G', 'CD', 2006)";
          try {
             stmt = con.createStatement();
             stmt.executeUpdate(addProduct1);
             stmt.executeUpdate(addProduct2);
             stmt.close();
             con.close();
          catch(SQLException ex) {
             System.err.println("SQLException: " + ex.getMessage());
       public static void addMembers(){
          Connection con = getConnection();
          String addMember1, addMember2;
          addMember1 = "insert into Member values  
          (1234, 'Ann', 'Smyth', 'Upper Killult, Falcarragh, Co. Donegal', '(074)-
          9135210', '(087)-2030172', #5/11/85#, #5/12/06#)";
          addMember2 = "insert into Member values
          (2345, 'John', 'Murphy', 'Lower Killult, Falcarragh, Co.
          Donegal', '(074)-9135211', '(087)-2030173', #4/12/85#, #6/13/06#)";
          try {
             stmt = con.createStatement();
             stmt.executeUpdate(addMember1);
             stmt.executeUpdate(addMember2);
             stmt.close();
             con.close();
          catch(SQLException ex) {
             System.err.println("SQLException: " + ex.getMessage());
       public static void addRentals(){
          Connection con = getConnection();
          String addRental1, addRental2;
          addRental1 = "insert into Rental values (110001, 'The Killers - 
          Sams Town', 1234, 'Ann', 'Smyth', #9/01/06#, #9/10/06#, 'Yes', 2.00)";
          addRental2 = "insert into Rental values (120001, 'Mission
          Impossible 3', 2345, 'John', 'Murphy', #9/02/06#, #9/09/06#, 'No',
          0.00)";
          try {
             stmt = con.createStatement();
             stmt.executeUpdate(addRental1);
             stmt.executeUpdate(addRental2);
             stmt.close();
             con.close();
          catch(SQLException ex) {
             System.err.println("SQLException: " + ex.getMessage());
       public Demo(){
          super("Demo Test Frame");
          setSize(350, 200);
          comboBox = new JComboBox();
          comboBox.addItem("jdbc:odbc:VideoLibrary");
          qtm = new QueryTableModel();
          JTable table = new JTable(qtm);
          JScrollPane scrollpane = new JScrollPane(table);
          JPanel p1 = new JPanel();
          p1.setLayout(new GridLayout(3, 2));
          p1.add(comboBox);
          p1.add(new JLabel("Enter your query: "));
          p1.add(queryField = new JTextField());
          p1.add(new JLabel("Click here to send: "));
          JButton jb = new JButton("Search");
          jb.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                      qtm.setHostURL();
                      qtm.setQuery(queryField.getText().trim());
          p1.add(jb);
          getContentPane().add(p1, BorderLayout.NORTH);
          getContentPane().add(scrollpane, BorderLayout.CENTER);
    }//end class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class LogOn extends JPanel{
       private static JTextField username=null;
       private static JPasswordField password=null;
       private static JButton button=null;
       public LogOn(){
          setSize(260,160);
          username=new JTextField(15);
          password=new JPasswordField(15);
          //JPanel pane=new JPanel();
          JLabel usernameLabel=new JLabel("Username: ");
          JLabel passwordLabel=new JLabel("Password: ");
          /*pane.*/add(usernameLabel);
          /*pane.*/add(username);
          /*pane.*/add(passwordLabel);
          /*pane.*/add(password);
          //setContentPane(pane);
          show();
          button=new JButton("Ok");
          add(button);
          button.addActionListener(new ButtonListener());
          JFrame frame=new JFrame("Welcome to Home Entertainment");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new LogOn());
          frame.pack();
          frame.setVisible(true);
       private class ButtonListener implements ActionListener{
          public void actionPerformed(ActionEvent e){
             try{
                new Demo();
             catch(Exception ex){}
    import java.sql.*;
    import java.io.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.*;
    public class QueryTableModel extends AbstractTableModel{
       Vector cache; 
       int colCount;
       String[] headers;
       Connection db;
       Statement statement;
       String currentURL;
       public QueryTableModel(){
          cache=new Vector();
          try{
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          catch(Exception e){
             System.out.println("problem loading the driver ");
       public String getColumnName(int i){
          return headers;
    public int getColumnCount(){
    return colCount;
    public int getRowCount(){
    return cache.size();
    public Object getValueAt(int row, int col){
    return ((String[])cache.elementAt(row))[col];
    public void setHostURL(){
    String url = "jdbc:odbc:VideoLibrary";
    closeDB();
    try{
    db=DriverManager.getConnection(url,"","");
    statement=db.createStatement();
    catch(Exception e){
    System.out.println("Could not initialize the database.");
    e.printStackTrace();
    public void setQuery(String q){
    cache=new Vector();
    String s="select * from Product";
    try{
    ResultSet rs=statement.executeQuery(q);
    ResultSetMetaData meta=rs.getMetaData();
    colCount=meta.getColumnCount();
    headers=new String[colCount];
    for(int h=1;h<=colCount;h++){
    headers[h-1]=meta.getColumnName(h);
    while(rs.next()){
    String[] record=new String[colCount];
    for(int i=0;i<colCount;i++){
    record[i]=rs.getString(i+1);
    cache.addElement(record);
    fireTableChanged(null);
    catch(Exception e){
    cache=new Vector();
    e.printStackTrace();
    public void initDB(String url){
    try{
    db=DriverManager.getConnection(url);
    statement=db.createStatement();
    catch(Exception e){
    System.out.println("Could not initialize the database.");
    e.printStackTrace();
    public void closeDB(){
    try{
    if(statement!=null){
    statement.close();
    if(db!=null){
    db.close();
    catch(Exception e){
    System.out.println("Could not close the current connection.");
    e.printStackTrace();

  • Swing related question wrongly posted in the JDBC forum

    The OP needs some sort of dynamic table model. He does not seem to understand the difference between JDBC and Swing so I am not hopeful for an easy resolution.
    The table model used appears to be static so it is not wonder that the new rows are not visible.
    I asked the OP to post their questiopn in here several times so far but it seems rather hopeless. Perhaps someone here can help.
    http://forum.java.sun.com/thread.jspa?threadID=699957&tstart=0

    If you have CC you get Edge and you are free to use it any DPS work you do, including Single Edition.
    Bob

  • JDBC-ODBC Bridge, Swing Components

    This program displays information from the Access database Autos.mdb.
    Uses the JDBC-ODBC Bridge.
    Requires a DSN called Autos pointing to Auto.mdb
    I am using the swing components and can't seem to set up my combobox to pull the right data which doesn't display any of the data from the database. I am able to do it using AWT but would like a sleeker look so I am trying to convert. I have made the text bold where I am having the problem. Any insight would be greatful! I am very new to JDBC-ODBC so please go easy on me...
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JDBCApp extends Frame implements ItemListener
         //Declare database variables
         Connection conVehicle;
         Statement cmdVehicle;
         ResultSet rsVehicle;
         boolean blnSuccessfulOpen = false;
         //Declare components
         JComboBox lstManuf   = new JComboBox();
         JLabel lblModel  = new JLabel("                          ");
         JLabel lblYear        = new JLabel("                          ");
         JLabel lblCost      = new JLabel("                          ");
         JLabel lblID      = new JLabel("                              ");
         JLabel lblInstructions = new JLabel("Select Manufacturer to Display Record");
         public static void main(String args[])
              //Declare an instance of this application
              JDBCApp thisApp = new JDBCApp();
              thisApp.createInterface();
         public void createInterface()
              //Load the database and set up the frame
              loadDatabase();
              if (blnSuccessfulOpen)
                   setTitle("Display Auto's Database");
                   addWindowListener(new WindowAdapter()
                             public void windowClosing(WindowEvent event)
                                  stop();
                                  System.exit(0);
                   setLayout(new FlowLayout());
                   add(new JLabel("Manufacturer"));
                   add(lstManuf);
                   lstManuf.addItemListener(this);
                   add(lblInstructions);
                   add(new JLabel("Model"));
                   add(lblModel);
                   add(new JLabel("Year"));
                   add(lblYear);
                   add(new JLabel("Cost"));
                   add(lblCost);
                   add(new JLabel("Vehicle Identification"));
                   add(lblID);
                   setSize(300,300);
                   setVisible(true);
              else
                   stop();             //Close any open connection
                   System.exit(-1);    //Exit with error status
              public void loadDatabase()
                   try
                        //Load the MicroSoft drivers
                        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
                   catch (ClassNotFoundException err)
                             //No drivers found
                             System.err.println("Driver did not load properly");
                   try
                        //Connect to the database
                        conVehicle = DriverManager.getConnection("jdbc:odbc:Auto");
                        //Create a ResultSet
                        cmdVehicle = conVehicle.createStatement();
                        rsVehicle = cmdVehicle.executeQuery(
                                            "Select * from Vehicle;");
                        loadNames(rsVehicle);
                        blnSuccessfulOpen = true;
                   catch(SQLException error)
                        System.err.println("Error: " + error.toString());
         public void loadNames(ResultSet rsVehicle)
              try
                   //Fill last name list box
                   while(rsVehicle.next())
                   lstManuf.addItem(rsVehicle.getString("Manufacturer"));
              catch (SQLException error)
                   System.err.println("Error in display record");
         public void itemStateChanged(ItemEvent event)
              //Display the selected record
              lblInstructions.setText("");          String strManufName = lstManuf.getText();
              try
                   Statement cmdVehicle = conVehicle.createStatement();
                   ResultSet rsVehicle = cmdVehicle.executeQuery(
                        "Select * from Vehicle where [Manufacturer] = '" + strManufName + "';");
                   DisplayRecord(rsVehicle);
              catch(SQLException error)
                   System.err.println("Error in recordset");
          public void DisplayRecord(ResultSet rsVehicle)
               try
                   //Display information
                   if(rsVehicle.next()) //If more records remain
                        lblModel.setText(rsVehicle.getString("ModelName"));
                        lblYear.setText(rsVehicle.getString("Year"));
                        lblCost.setText(rsVehicle.getString("CostValue"));
                        lblID.setText(rsVehicle.getString("VehicleID"));
                   else
                        System.err.println("No more records");
              catch (SQLException error)
                   System.err.println("Error in display record");
         public void stop()
              try
                   //Terminate the connection
                   if (conVehicle != null)
                        conVehicle.close();
              catch(SQLException error)
                   System.err.println("Unable to disconnect");
    }

    I am going to help you out but first I would like to advise you not to do this.
    Mixing JDBC and Swing (or any GUI for that matter) like this is not the preferred way to proceed. If you want to learn JDBC then command line/shell is fine. If you want to learn Swing learn Swing. When you want to use the two together learn about MVC first.
    I am advising you to do this because nobody writes code like this (mixing GUI and database and business logic all in one class). So if you want real world training MVC should be what you look at next.
    See http://en.wikipedia.org/wiki/Model-view-controller for more
    As near as I can tell your problem is mostly just because you aren't using the combo box correctly. Try
    String strManufName = lstManuf.getSelectedItem().toString();it's a bit of a hack but will work for your purposes. What you need in the eend is to use the methods JComboxBox has like getSelectedItem() or getSelectedIndex().
    See http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html for more.
    Some other comments.
    1) It's nice to see you aren't just swallowing exceptions but some of them are kind of pointless if you just continue on. Like here
    try
      //Load the MicroSoft drivers
      Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    catch (ClassNotFoundException err)
      //No drivers found
      System.err.println("Driver did not load properly");
    }And then you continue on with the method. What for? There's no point since the driver didn't load.
    2) Please look into the use of PreparedStatements. Very good things those. You can use them in place of your Statements. They are safer but for your purposes help you by you not having to worry about the formatting of data you bind to queries. For example what happens if a manufacturer name contains a ' (single quote) ? Trouble that's what. PreparedStatements make that problem go away.
    3) Don't use SELECT *. It is always good practice to put the names of the columns you are selecting. This prevents your code from breaking if the order of the columns should change in any way.

Maybe you are looking for