Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use datas from login panel in another class  RSS feed

 
Karol Dziedzic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting datas from user and checking it with database (MS SQL ) if it fits it is sweatching to new window. In new window I need to use again my database so I need to add connecting datas again. I would like to get them from login window. How to do that? I know theory but when I am writting code nothing works. Here is working code for login panel :

/*import java.awt.BorderLayout;

import javax.swing.*;

public class Login extends JFrame {
public String url;
public static void main(String[] args) {
Log frameTabel = new Log();
}

JButton log = new Jbutton("Log");

JPanel panel = new JPanel();

// --- (….) labels------

JTextField DB = new JTextField(15);
JTextField user = new JTextField(15);
JPasswordField password = new JPasswordField(15);

Login(){
super("Login");
setSize(400,300);
setLocation(300,10);
panel.setLayout (null);

DB.setBounds(50,100,261,25);
user.setBounds(50,135,261,25);
password.setBounds(50,170,261,25);

log.setBounds(50,213,150,25);

panel.add(DB);
panel.add(user);
panel.add(password);
panel.add(log);

DB.setDocument(new LogLimit(10));
user.setDocument(new LogLimit(10));
password.setDocument(new LogLimit(10));

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin(); }

public void actionlogin(){
loguj.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

String aip1 = // add;
String aserver = // add;
String aDB = DBy.getText();
String auser = user.getText();
String apassword = password.getText();

Connection connection = null;

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

String url = "jdbc:sqlserver://"+ aip +
"databaseName="+aDB+";user="+auser+";
password="+apassword+";";;

connection = DriverManager.getConnection(url);

NextWindow regFace =new NextWindow();
regFace.setVisible(true);
dispose();

}
catch (ClassNotFoundException e) {

System.out.println("Engine not found" + e.getMessage());

JoptionPane.showMessageDialog(null,"Can not find engine ");
DB.setText("");
user.setText("");
password.setText("");
user.requestFocus();
}
catch (SQLException e) {

System.out.println("Can not conncet " + e.getMessage());

JoptionPane.showMessageDialog(null,"Can not conncet ");

DB.setText("");
user.setText("");
password.setText("");
user.requestFocus();
}}});
}}
 
m beardsley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use code tags, it's much easier to understand what you're working on.

You need to save the database connection object (and any thing else that you need access to in other classes) somewhere and provide access to them. For larger projects, I'd use an "application" class to coordinate the display and workflow of your GUI. For a small project, just add the connection object as a class variable and make a public accessors (get/set) so you can get to it from other screens.

For a more complete solution, take a look at https://kenai.com/projects/bsaf/pages/Home

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!