2 Replies - 1321 Views - Last Post: 05 April 2011 - 06:44 AM Rate Topic: -----

#1 seaneyb   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Java Jtext & MYSQL Statement

Posted 04 April 2011 - 03:18 PM

Im having a bit of a situation at the minunte i want to basically be able for a person to enter a username and password via a little jframe to connect to a background database to retrieve X Y values

Now im new to MYSQL statements i have written the code below i know the sql statement wont pickup the values in the text box. i just dont know how to do it, ive done it in PHP where its just a post varible and you use something like this:

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT boilertemp, boilerstatus FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";


public void internetaccesspanel()
{
	frame2 = new JFrame();
	frame2.setTitle("Zigbee Thermostat Control");
	frame2.setSize(200, 200);

	//sets up panel
	panel2 = new JPanel();
	panel2.setBackground(Color.WHITE);
	panel2.setLayout(null);
	frame2.getContentPane().add(panel2);
	frame2.setVisible(true);
	panel2.setVisible(true);
	
	username = new JLabel("Username:");
	username.setBounds(0,2,100,100);
	panel2.add(username);
	password = new JLabel("Password:");
	password.setBounds(0,30,100,100);
	panel2.add(password);
	
	//Add text field
	txtusername = new JTextField();
	txtusername.setBounds(65,45,80,20);
	panel2.add(txtusername);
	//Add text field
	txtpassword = new JTextField();
	txtpassword.setBounds(65,70,80,20);
	panel2.add(txtpassword);
	
	connect = new JButton("Connect");
	connect.setBounds(55,100,85,30);
	panel2.add(connect);
	connect.addActionListener(new ActionListener()
	{public void actionPerformed(ActionEvent event) 
	{
		JavaMySQL();
	}
	});
	
}

public void JavaMySQL() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            String dbURL = "jdbc:mysql://!!!";
            String username = "!!";
            String password = "!!";
            String database = "!!n";

            Class.forName("com.mysql.jdbc.Driver");;

            conn =
                DriverManager.getConnection(dbURL+database, username, password);

            stmt = conn.createStatement();

            if (stmt.execute("SELECT boilertemp, boilerstatus FROM members WHERE username = 'txtusername' AND password = 'txtpassword'")) {
                rs = stmt.getResultSet();
               
            } 
            
            else {
                System.err.println("select failed");
            }
            while (rs.next()) {
                String entry = rs.getString(1);
                String entry2 = rs.getString(2);
                System.out.println(entry);
                System.out.println(entry2);
            }

        } catch (ClassNotFoundException ex) {
            System.err.println("Failed to load mysql driver");
            System.err.println(ex);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage()); 
            System.out.println("SQLState: " + ex.getSQLState()); 
            System.out.println("VendorError: " + ex.getErrorCode()); 
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) { /* ignore */ }
                rs = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) { /* ignore */ }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) { /* ignore */ }
                conn = null;
            }
        }
    }


How do i achieve this in Java? bit miffed right now :((

Is This A Good Question/Topic? 0
  • +

Replies To: Java Jtext & MYSQL Statement

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Jtext & MYSQL Statement

Posted 04 April 2011 - 03:30 PM

Just pass the 2 JtextField as parameter to your JavaMySQL() method
and use entry and entry1 to setText() to them
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Jtext & MYSQL Statement

Posted 05 April 2011 - 06:44 AM

Also, a couple things about refactoring your code. First, if a GUI Component requries a method to set it up, then it has enough code to warrant subclassing the Component (in this case JFrame). It will allow you to write a cleaner, more modular, and more maintainable GUI.

Also, always separate your program user interface from your state/data. There should be a DataManager or StateManager class that manages the state of the program, as well as the manages your database connections. Your GUI should update the appropriate fields in the StateManager, which should then update the database.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1