3 Replies - 1336 Views - Last Post: 12 October 2012 - 07:28 PM Rate Topic: -----

#1 grendos2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 03-November 09

problem connecting to database using Java and jFrame

Posted 12 October 2012 - 06:25 PM

Anyone with a sharp eye spot anything? I cant seem to make a connection.
I have a database called test, a table called login and columns username and password.
The query works fine if i hard code it in sql

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class LoginDemo extends JFrame{
 JButton SUBMIT;
 JLabel label1,label2;
 final JTextField  text1,text2;
  LoginDemo(){
    setTitle("Login Form");
    setLayout(null);
    label1 = new JLabel();
    label1.setText("Username:");
    text1 = new JTextField(15);

    label2 = new JLabel();
    label2.setText("Password:");
    text2 = new JPasswordField(15);

    SUBMIT=new JButton("SUBMIT");
    label1.setBounds(350,100,100,20);
    text1.setBounds(450,100,200,20);
    label2.setBounds(350,130,100,20);
    text2.setBounds(450,130,200,20);
    SUBMIT.setBounds(450,160,100,20);
   add(label1);
   add(text1);
   add(label2);
   add(text2);
   add(SUBMIT);

   setVisible(true);
   setSize(1024,768);

 SUBMIT.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent ae){
    String value1=text1.getText();
    String value2=text2.getText();
    try{
 Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+value1+"' and password='"+value2+"'");
           String uname="",pass="";
           if(rs.next()){
               uname=rs.getString("username");
               pass=rs.getString("password");
           }
 if(value1.equals("") && value2.equals("")) {
      JOptionPane.showMessageDialog(null,"Enter login name or password","Error",JOptionPane.ERROR_MESSAGE);
  }
 else if(value1.equals(uname) && value2.equals(pass)) {
    NextPage page=new NextPage(uname);
    page.setVisible(true);
    }
 else{
     JOptionPane.showMessageDialog(null,"Invalid login name or password","Error",JOptionPane.ERROR_MESSAGE);
     text1.setText("");
     text2.setText("");
  }
    }
    catch(Exception e){}
}
 });
  }
  public static void main(String arg[]){
  new LoginDemo();
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: problem connecting to database using Java and jFrame

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: problem connecting to database using Java and jFrame

Posted 12 October 2012 - 06:44 PM

Anything? Wrong forum, moving on over to Java
Was This Post Helpful? 0
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: problem connecting to database using Java and jFrame

Posted 12 October 2012 - 06:58 PM

What error are you getting?
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: problem connecting to database using Java and jFrame

Posted 12 October 2012 - 07:28 PM

catch(Exception e){}



Print your exceptions. They'll tell you what's wrong. Also, is this a connection error or an authentication error?

This post has been edited by blackcompe: 12 October 2012 - 07:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1