Closed Thread
Results 1 to 2 of 2

Thread: displaying mysql data into jtextfield.Pls help

  1. #1
    iz.ziffa Guest

    displaying mysql data into jtextfield.Pls help

    Hi guys.I have a problem.I just created the connection class and it works perfect for sure.Now i have a problem.I have a mysql database named tbl_login and in it there is 2 columns which are Username and password. Now in the java program i created 2 jtextfields and i want to view the username and password i created into the database into the 2 jtextfields. How im gonna do it? Below you will find my connection coding, tks:
    Code:
    package project1;
    
    import com.mysql.jdbc.Connection;
    
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    
    import com.mysql.jdbc.Statement;
    
    
    import javax.swing.JOptionPane;
    public class COnnection {
        
        
            static Connection connection;
            static Statement statement;
            static ResultSet myResult = null;
            
        public static ResultSet getResultsFromQuery(String sqlQry) {
                connection = null;
                statement = null;
                myResult = null;
    
                connection = getConnection();
                try {
                    //instanciation of statement
                    statement = (Statement)connection.createStatement();
                    //executing the statement
    
                    myResult = statement.executeQuery(sqlQry);
    
    
                } catch (SQLException f) {
                    //displaying an error popup window
                    JOptionPane.showMessageDialog(null, f.toString());
                    f.printStackTrace();
                }
                return myResult;
            }
    
        
        private static Connection getConnection() {
    
                //declaration of the connection
                connection = null;
    
                try {
                    //load the j/connector driver
                    Class.forName("com.mysql.jdbc.Driver");
    
                    //creating the connection to the database
                    //connection properties
                    String server = "localhost";
                    String database = "mydatabase";
                    String url = "jdbc:mysql://" + server + "/" + database;
    
                    String username = "renezamm";
                    String password = "123";
    
                    connection = 
                            (Connection)DriverManager.getConnection(url, username, password);
    
                } catch (ClassNotFoundException e) {
                    System.out.println("could not find the database driver");
                } catch (SQLException e) {
                    System.out.println("Could not connect to the database");
                }
    
                // since this method returns a connection we are goint to return the created conenction
                return connection;
            }
    
            /**
             * this method is used to execute a query
             * @param myQuery enter the query you wish to execute here
             */
            public static void excecuteQuery(String myQuery) {
                connection = getConnection();
    
    
                try {
                    statement = (Statement)connection.createStatement();
                    statement.execute(myQuery);
    
                } catch (SQLException e) {
                    System.out.print(e);
                }
            }
    
            /**
             * this method is used to see the records within a table
             * @param tableName the name of the table of which you want to see the data
             * @return a resultset containing all requested data of the given table 
             */
            public static ResultSet viewRecords(String tableName) {
                connection = getConnection();
                try {
                    //instanciation of statement
                    statement = (Statement)connection.createStatement();
                    //executing the statement
    
                    String sqlQry = "Select * from " + tableName;
                    myResult = statement.executeQuery(sqlQry);
    
    
                } catch (SQLException f) {
                    //displaying an error popup window
                    JOptionPane.showMessageDialog(null, f.toString());
                    f.printStackTrace();
                }
                return myResult;
            }
        
        
    
        public COnnection() {
        }
    Last edited by v0id; 05-27-2007 at 11:37 AM. Reason: Added code-tags.

  2. CODECALL Circuit advertisement

     
  3. #2
    oubless is offline Newbie
    Join Date
    May 2007
    Posts
    22
    Rep Power
    0
    I haven't read all your code, still I can try and give you few hints:
    1. Why do you include com.mysql.xxx ...
    Usually you need java.sql.xxx alternatives. ( xxx is Connection and Statement )
    2. Consider separating sql queries from visualization -
    make a class that will query you database for the information and will have getters and another class that will contain the GUI code and will use the getters of the first class - this will make it easier to read your code and fix bugs/mistakes (If you are not in a hurry read about Model-View-Controller).
    Sorry, I don't have time right now to supply a working code fragment...

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help at the displaying data from database
    By Epatron in forum PHP Development
    Replies: 9
    Last Post: 10-08-2011, 12:55 PM
  2. Displaying image and data from different tables
    By kirigwajoe in forum PHP Development
    Replies: 6
    Last Post: 01-27-2011, 04:26 PM
  3. Displaying MySQL data in HTML with PHP
    By dunnkers in forum PHP Development
    Replies: 4
    Last Post: 07-16-2010, 08:53 AM
  4. Displaying records from a mySQL DB
    By aakinn in forum PHP Development
    Replies: 37
    Last Post: 03-26-2010, 01:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

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