1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

displaying MYSQL data into jtextfield.Pls help

Discussion in 'Java' started by iz.ziffa, May 27, 2007.

  1. iz.ziffa

    iz.ziffa New Member

    Joined:
    May 27, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: May 28, 2007

Share This Page