Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Program not reading my Database

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Program not reading my Database

    I'm trying to make my program read from a database. I've used the following code given by my online course to read from a database:

    import java.sql.*;
     
    public class Database
    {
    	private Connection conn = null;
    	ResultSet rs;
    	public Database()
    	{
    		try
    		{
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			conn = DriverManager.getConnection(
    			   "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=PAT3");
    			System.out.println("Connection successful");
    		}
    		catch(Exception e)
    		{
    			System.out.println("Failed to get connection");
    			e.printStackTrace();
    		}
    	}

    Now, in my program, I'm trying to make a button do work:
    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {                                      
            // TODO add your handling code here:
            inputProductID.setText(PAT3.updateTbl("INSERT INTO TransactionDetails(CustomerID, ProductID, UnitPrice, Quantity)"
                    +Integer.parseInt(lblCustomerID.getText())+
                    "','"+Integer.parseInt(inputProductID.getText())+
                    "','"+Integer.parseInt(lblUnitPrice.getText())+
                    "','"+Integer.parseInt(inputQuantity.getText())+"';"));
            setVisible(false);
            JOptionPane.showMessageDialog(null,"Transaction has successfully been completed");
        }

    In line 3, PAT3 is being underlined by my JDE (Netbeans) and the program is not running whenever I click this button.

    I am having similar problems with data-retrievement throughout my program. I have entered the stuff into the ODBC here: itQuestion1.jpg and also put it into the file DSN after the first it didn't work in the user DSN.

    Can anyone tell me what I'm doing wrong?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Program not reading my Database

    For problems like this, it is always best to show us the error message that your compiler gives you. I can guarantee you that NetBeans is giving you more than just a word being underlined. It looks to me though that you're trying to use PAT3 as a variable, but that it has not been declared as a variable anywhere. Also, does your program have an updateTbl method declared anywhere?

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Program not reading my Database

    In this particular example, it is just PAT3 that is being underlined. The Error messages are:

    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code
    at makeTransaction.btnOKActionPerformed(makeTransacti on.java:132)
    at makeTransaction.access$000(makeTransaction.java:20 )
    at makeTransaction$1.actionPerformed(makeTransaction. java:60)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
    I assumed PAT3 was declared and made accessable by the first batch of code?

    I don't have an updateTbl method declared anywhere, which makes it weird that it isn't underlined. I assumed it was a method within java.sql.* seeing as it wasn't flagged as an error.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Program not reading my Database

    Quote Originally Posted by EagerBrad View Post
    In this particular example, it is just PAT3 that is being underlined. The Error messages are:
    The message above just tells us that you're trying to run uncompilable code. Don't do this, but instead just give us the error that the compiler gives you. With Eclipse you can see this by hovering your mouse over the code marked in error, and I'll bet that NetBeans is similar.


    I assumed PAT3 was declared and made accessable by the first batch of code?
    No. This:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection(
    			   "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=PAT3");

    Creates a Connection object that is hopefully connected to your PAT3 database. No where is there a variable declared with the name PAT. The only variable initialized here is the conn variable. Consider going through the Oracle Java JDBC tutorial for the necessary details on what to do.



    I don't have an updateTbl method declared anywhere, which makes it weird that it isn't underlined. I assumed it was a method within java.sql.* seeing as it wasn't flagged as an error.
    The Java API and the tutorials will tell you what methods are available. You will want to refer to them often as otherwise you risk making up methods that don't exist, and that never works.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Program not reading my Database

    Quote Originally Posted by curmudgeon View Post
    The message above just tells us that you're trying to run uncompilable code. Don't do this, but instead just give us the error that the compiler gives you. With Eclipse you can see this by hovering your mouse over the code marked in error, and I'll bet that NetBeans is similar.
    Ah, that message is:

    "cannot find symbol
    symbol: variable PAT3
    location: class makeTransaction"

    No. This:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection(
    			   "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=PAT3");

    Creates a Connection object that is hopefully connected to your PAT3 database. No where is there a variable declared with the name PAT. The only variable initialized here is the conn variable. Consider going through the Oracle Java JDBC tutorial for the necessary details on what to do.
    Why would I be making a variable though? All I want to do is connect to the database, which is what this does.


    The Java API and the tutorials will tell you what methods are available. You will want to refer to them often as otherwise you risk making up methods that don't exist, and that never works.
    Yeah I know, but according to netbeans there is nothing wrong with that method, which is confusing me.

Similar Threads

  1. how to plot a line graph by reading the values from database?
    By priti in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2012, 01:39 AM
  2. Need help with coding - Vocabulary Database Program
    By wogns10 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 20th, 2012, 03:40 AM
  3. simple program for reading text file
    By johnpipes in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2011, 04:55 PM
  4. Speed reading program j2me
    By VeliDemir in forum Java SE APIs
    Replies: 1
    Last Post: May 16th, 2011, 08:17 AM
  5. Reading CSV files into a program
    By Wrathgarr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2010, 10:41 AM