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

New Topic/Question
Reply


MultiQuote






|