Hi everyone,
I am trying to make a user login page.
The user is required to fill username and password in textFields and when he clicks the Submit button, all details are needed to be written in the database.

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class register  {

   JFrame f;
   JLabel l1,l2;
   JTextField f1,f2;
   JButton b1;
    Connection con;
    Statement st;
    ResultSet rs;

    /**
     * Creates new form Register
     */
    public static void main(String args[]) {
       register r=new register();
        r.frame();
        r.connect();
    }

    public void connect() {
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:sd","SYSTEM","monika");
            st = (Statement) con.createStatement();
            String str = "insert into Table1 values('&userName','&passWord')";
            st.executeUpdate(str);
        } catch (Exception ex) {
        }


    }

    public void frame() 
    {
     f=new JFrame ("Login");
     f1=new JTextField(30);
     f2=new JTextField(30);
     l1=new JLabel("username");
     l2=new JLabel("password");
     b1=new JButton("submit");
     f.setLayout(new GridLayout(3,2));
     f.add(l1);
     f.add(f1);
     f.add(l2);
     f.add(f2);
     f.add(b1);
     f.setVisible(true);
     f.pack();
     b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            String userName=l1.getText();
            String passWord=l2.getText();
            connect();
        }
    });
    }
    }

I am getting troubled in the line

        String str = "insert into Table1 values('&userName','&passWord')";

As it is updating the database table with values &userName and &passWord.
Please help me in this...
:(

hai adikimicky,

suggestions (for better programing):

make your connect method signature as follows

public void connect(String user_name,String pass){
//  your code here
}

at line 59 pass username and password values to connect() method.so that line will be as follows

connect(userName,passWord);

then finally chage the query as follows

String str = "insert into Table1 values('"+user_name+"','"+pass+"')";

thats it

happy coding

also: don't make it harder on yourself then it should be:

catch (Exception ex) {
        }

see what's wrong there?
how will you ever know whether that part of your code runs the way you think, or throws a whole lot of exceptions?

you are hiding every exception you throw, which is very bad to do.
as long as you are in development, print your stacktrace there, or add the stacktrace to a log file.

as soon as you go into production (actually go and use your code), add an understandable error message for the user, so at least you/he'll be warned if something wrong happend there.

commented: ohke sir..I will take care of your advice. +0

@RadhaKrishna mam
I am still getting an error at line 22
r.connect(userName,passWord);

The error is

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable userName cannot be referenced from a static context
    at register.main(register.java:22)

ohh in the above post I was doing a mistake.
Here is the full correct code but still it is not writing the values that are provided as input at run time. Still it is printing username and password in the respective columns of the table.

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class register  {
     JFrame f;
   JLabel l1,l2;
   JTextField f1,f2;
   JButton b1;
    Connection con;
    Statement st;
    ResultSet rs;

    /**
     * Creates new form Register
     */
    public static void main(String args[]) {

       register r=new register();
        r.frame();

    }

    public void connect(String user_name,String pass) {
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:sd","SYSTEM","monika");
            st = (Statement) con.createStatement();
            String str = "insert into Table1 values('"+user_name+"','"+pass+"')";
            st.executeUpdate(str);
        } catch (Exception ex) {
            ex.printStackTrace();
        }


    }

    public void frame() 
    {
     f=new JFrame ("Login");
     f1=new JTextField(30);
     f2=new JTextField(30);
     l1=new JLabel("username");
     l2=new JLabel("password");
     b1=new JButton("submit");
     f.setLayout(new GridLayout(3,2));
     f.add(l1);
     f.add(f1);
     f.add(l2);
     f.add(f2);
     f.add(b1);
     f.setVisible(true);
     f.pack();
     b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
         String userName=l1.getText();
         String passWord=l2.getText();
            connect(userName,passWord);
        }
    });
    }
    }

Do help me..

please help Me..:(

String userName=l1.getText();
         String passWord=l2.getText();
            connect(userName,passWord);

l1 and l2 are two JLabels containing "username" and "password".

which you pass on to your connect method where you do this:

String str = "insert into Table1 values('"+user_name+"','"+pass+"')";

so, at that line, the value of user_name = "username" and the value of pass = "password".

ohhhhh...
I am really a foool..
Instead of textFields , I am taking input of JLabels...

Thanks a lottt..:)

Ther was way to add username password. First i have connected to localhost database using host as 127.0.0.1. now i am trying to connect to 54.35.xx.xxx Mysql database but i m getting this error.

ERROR java.sql.SQLException: Access denied for user 'root'@'50.16.35.xxx' (using password: NO)

In error massage its trying to connect my local machine again because my local machine IP address is 50.16.35.xxx instead of 54.35.xx.xxx

Can anyone please tell me how this is happening. Plz help me : lakhveer.singh51@gmail.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.