Results 1 to 7 of 7
  1. #1
    staticy2003 is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Unhappy Java program that stores user inputs

    Hello
    I am almost new in Java, and i have to do a program in java that acquires the user inputs from a file or from the interaction with the GUI, and stores these data in a database.
    I installed MYSQL, since it is opensource...
    Anyone has any suggestion?
    Thanks in advance

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Welcome to the Java Forums!

    Have you attempted writing the code yet? If so, please post it if you get stuck - and please use code tags. You can start by reviewing both the Database forums for some posts related to yours, and/or have a view at this site's Tutorials where there are a few threads regarding databases.

    MySQL is a good choice.. however, you say you're "almost new", and I'm not sure what you mean by that. For beginner Java programmers, this assignment you have to do requires the use of some more advanced concepts. But post your code and ask your specific questions, we're here to help.

    See you around!
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Before writing the program you have to create a DSN and then create tables in that particular DSN and then carry on with the code, also set the classpath for the database.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First create required tables and stuff in the database first.

    Then think that how to connect database to a Java application. At that point you can learn a lot about database connections. There few number of connections are available for Java.

    Then try to find out that how to write data to a database table. Just use some values which is on your application itself included. Don't think about GUI and user command line inputs, etc.

    At last try to work with user inputs.

    Likewise try to step out your question first. Then try to do something. Then put your code here and all the members help you to solve your question. Don't worry about that you are a newbie for Java. This project is a very good one to start work with Java. Try and see.

    Good luck pal...

  5. #5
    staticy2003 is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default

    Thank you very much for your answers. It helped me a lot. Now I am going to try the different ways for doing this program and in anycase, if something doesn't work I'll put the code here.
    Thanks a lot

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    JAdmin is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    Here is a sample code to connect to a MySQL database

    Java Code:
        Database name     : yourdatabase
        Databse user      : aUser
        Database password : aPwd
    
        import java.sql.*;
     
        Connection conn = null;
        Statement stmt = null;
        ResultSet  rs = null;
      
        try{
          Class.forName(com.mysql.jdbc.Driver);       
          conn = 
          DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase",aUser,aPwd);
    
           //Create a Statement object.  
           stmt = conn.createStatement();  
           //Get a ResultSet  
            rs = stmt.executeQuery("select * from users");
            //Loop through the results 
            while(rs.next()){          
              String userName = rs.getString("user");
              System.out.println(userName );
            }
          }catch( Throwable t){  
           //handle error here  
            t.printStackTrace();
          }
          finally{       
            //note the sequence of closing the objects. rs first 
            // followed by stmt and connection
            try{
    	   if(rs != null) rs.close();
      	}catch(Throwable t){t.printStackTrace();}
     
     	try{
     	   if(stmt != null) stmt.close();
    	}catch(Throwable t){t.printStackTrace();}
    	
    	try{
     	   if(con != null) con.close();
    	}catch(Throwable t){t.printStackTrace();}
        }
    Always remember to close your objects in a finally block. That is the time tested best practice.

    Hope this helps!

    Sincerely, your friends at JavaAdvice.com
    Last edited by JAdmin; 01-25-2008 at 12:06 AM.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  2. Replies: 0
    Last Post: 03-31-2008, 04:35 PM
  3. Replies: 0
    Last Post: 02-06-2008, 02:07 PM
  4. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 05:11 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •