2

I created a java login frame using netbeans and I connected it to MySQL using MySQL Connector/J which the jar file was added to the projects library. I also created a table called login which includes all the login details. The following codes are supposed to permit login but I keep getting errors as if the connection to the database was not established.

package Lightapp;
import java.sql.* ;
import javax.swing.* ;

public class AbbeyLog extends javax.swing.JFrame
{
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    /**
     * Creates new form AbbeyLog
     */
    public AbbeyLog()
    {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void textuserActionPerformed(java.awt.event.ActionEvent evt)
    {
        // TODO add your handling code here:
        String sql = "select * from login where username = ? and password = ?";
        try
        {
            pst = conn.prepareStatement(sql);
            pst.setString(1, textuser.getText());
            pst.setString(2, textpass.getText());
            rs = pst.executeQuery();
            if (rs.next())
            {
                JOptionPane.showMessageDialog(null, "Username and Password correct");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "invalid username and password");
            }
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    private void textuserMouseClicked(java.awt.event.MouseEvent evt)
    {
        // TODO add your handling code here:
    }


    public static void main(String args[])
    {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new AbbeyLog().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField textpass;
    private javax.swing.JButton textuser;
    // End of variables declaration
}
1
  • 1
    Where have you given connection information? like server name, database name etc? Commented Dec 20, 2012 at 14:10

2 Answers 2

2

You need to create a jdbc connection at startup:

//load the driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
// create a connection         
conn = DriverManager.getConnection("jdbc:mysql://"+hostName+":"
                + dbPort+"/"+databaseName+"?"+"user="+dbUser+"&password=" + dbPassword);
Sign up to request clarification or add additional context in comments.

2 Comments

ive tried it your was but i keep getting thos error Java.sql.sqlexception: no suitable driver found
1. Put it in the constructor or in a method you call when starting up your client. 2. Please check the mysql connector/j (mysql-connector-java-5.1.22-bin.jar) is in your project (subfolder "libraries"). See here for more details: dev.mysql.com/doc/refman/5.1/en/…
0

A good way to do it is to create a separated file called dbConnection for eample. This file will contain method to connect to the DB. Typically it will look like this :

public class DBConnection{

  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;

public DBConnection() {}

public void connect() {
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:jtds:sqlserver://adress:port;DatabaseName=dbName", "login", "passwd");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Then in your main class you do :

dbConnection db=new dbConnection();
db.connect;

2 Comments

ive tried it your was but i keep getting thos error Java.sql.sqlexception: no suitable driver found
You need to have the jar file corresponding to your drivers. If you have it in the right folder and it still dosen't work you can try with other driver, for example the ones proposed by sourceforge (see my post)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.