Jump to content

Netbeans Login Form With Mysql Database, Got Questions Dont Know How To Form Them..


iNko

Recommended Posts

Hey, im using NetBeans to make a simple GUI, that would connect to a mysql database. I followed some guides and managed to make something, but now i want to change some features and i dont know what to look for on google/youtube.. Ill try to explain what i need, maybe someone can help me with it or point me in the right direction on what i should be looking for.

 

So, when i press 'Run Project' in NetBeans, 1st i get this:

2zre5bq.png

1) I would like to put that info message window, where it says that connection to database is established, somewhere in the GUI, is that possible?

 

After i press 'OK' on the info message window, i can then input my login/password and login into the databse, heres the screen:

2d0jkfp.png

2) Here the info message window says that the login information is correct, again, i would like to hide this info window so that it would display somewhere in the GUI and not as a pop-up window.

 

After i press 'OK' on the login detail window, a new 'JFrame' opens up (witch i will fill in with other things that i need later), heres the screen:

24ci711.png

3) The old 'Login form' window didnt dissapear, can i make it so the new 'JFrame' opens up and the login frame dissapears, or can i make it so that the new JFrame window would replace the login frame?

 

 

Heres the code just in case:

Settings for database, shows the 1st info message window (if connection was succesfull):

public class mysqlconnect {
Connection conn = null;
public static Connection ConnectDb() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://Database related stuff");
JOptionPane.showMessageDialog(null, "Prisijungimas pavyko");
return conn;
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}

 

When i click the 'Login' button in the login frame, shows if login info was correct or not, also if correct opens new JFrame:


private void cmdloginMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String sql = "select * from Vartotojas where login = ? and password = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtusername.getText());
pst.setString(2, txtpassword.getText());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys teisingi");
QueryJFrame s = new QueryJFrame();
s.setVisible(true);
}
else
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys neteisingi");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}

So yeah, these are my main problems at the moment, i dont really know what to type into google for help, so hoping someone here will clear things for me..

 

Also got some side questions:

4) After im done with this project, can i make it so it would be installable program?

5) If i can install it, can i make it start-up automatically every time i turn on the computer? (for example, like any antivirus program or even skype)

6) If i can install it, can it have its own icon near the clock in the bottom right corner?

 

Thx and sorry if this was posted in the wrong seciton.

Link to comment
Share on other sites

To answer questions 1, 2, & 3 when you create your GUI you should be creating a separate JPanel and adding it to your JFrame. You can update them dynamically in your Listener methods. If you want specifics post your code with the main JFrame container and where these methods reside.

Link to comment
Share on other sites

To answer questions 1, 2, & 3 when you create your GUI you should be creating a separate JPanel and adding it to your JFrame. You can update them dynamically in your Listener methods. If you want specifics post your code with the main JFrame container and where these methods reside.

 

Thx for helping, i have only 2 jframes now, ill post both of their codes because im not sure witch one is the main one

 

This is the one that opens after succesfull login:

public class QueryJFrame extends javax.swing.JFrame {

public QueryJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")

private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new QueryJFrame().setVisible(true);
}
});
}
}

 

And this is the code from the login frame


import java.sql.*;
import javax.swing.*;
public class loginJFrame extends javax.swing.JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;


public loginJFrame() {
initComponents();
}

@SuppressWarnings("unchecked")


private void formWindowOpened(java.awt.event.WindowEvent evt) {
conn = mysqlconnect.ConnectDb();
}

private void cmdloginMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String sql = "select * from Vartotojas where login = ? and password = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtusername.getText());
pst.setString(2, txtpassword.getText());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys teisingi");
QueryJFrame s = new QueryJFrame();
s.setVisible(true);
}
else
JOptionPane.showMessageDialog(null, "Prisijungimo duomenys neteisingi");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}

public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(loginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new loginJFrame().setVisible(true);
}

});
}


private javax.swing.JButton cmdlogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField txtpassword;
private javax.swing.JTextField txtusername;
}

 

And this is connection to database file


import java.sql.*;
import javax.swing.*;
public class mysqlconnect {
Connection conn = null;
public static Connection ConnectDb() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://database info");
JOptionPane.showMessageDialog(null, "Prisijungimas pavyko");
return conn;
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}

 

This is my entire code, ill go look into that JPanel attachment to JFrame thing now.

Edited by iNko
Link to comment
Share on other sites

Hmm idk why i cant edit my earlier post twice, so making a new post now..

 

Ok so i managed to get what i want with the info message windows by just adding a new label field into the GUI and changing:

 

This

//JOptionPane.showMessageDialog(null, "Prisijungimo duomenys teisingi"); 

 

Into this

logincheckLabel.setText("Prisijungimo duomenys teisingi");

 

Still cant find anything with the JFrame..

Link to comment
Share on other sites

.. Cant edit my previous post, so making new one again.....

 

Solved the jframe thingy with by making new method in the login JFrame:


  public void closeLogin() {
      WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
      Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
  } 

 

And then called this method after the login button is pressed:


try {
           pst = conn.prepareStatement(sql);
           pst.setString(1, txtusername.getText());
           pst.setString(2, txtpassword.getText());
           rs = pst.executeQuery();
           if (rs.next()){
               closeLogin();
               QueryJFrame s = new QueryJFrame();
               s.setVisible(true);

           }

 

Thx guys :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.