Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I am very confused in doing my project. I am trying to work with a database. I want to enter information in the frame (UI) and store this information in a database.. and other ways I want to show in the UI which information (like name surname or email address) already stored in the database. But I don't know how to "combine" both classes and I am not sure if this the right way to that.

<pre lang="java">import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.JOptionPane;

import com.mysql.jdbc.PreparedStatement;

public class laborversuch extends fensterkomponente{
	static Connection con;
	static PreparedStatement  ps;
	static ResultSet rs;
	static Statement st;
	//static String name ;
    
	public static void main (String[] args) 
   
    {

    	//String x = txtfldVorname.getText();
 		 try {
 			   Class.forName("com.mysql.jdbc.Driver");
 			   Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/dblabor","root",""); 
 			   System.out.println("Connection successful");
 			
 		    		String sql = "SELECT name,nachname FROM kunden";
 		    		ps= (PreparedStatement) con.prepareStatement(sql);
 						/*ps.setString(1,txtfldVorname.getText());		*/
 						
 						 rs = ps.executeQuery();
 						  
 						 if(rs.next()){
 							   String name = rs.getString("name");
 							   String nachname = rs.getString("nachname");
 							  System.out.print("Name: " + name);
 							 System.out.print(" Nachname: " + nachname);
 							}
 						 
 						 
 						  rs.close();


Other class for the User Interface

import javax.swing.*;

import com.mysql.jdbc.PreparedStatement;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.jar.Attributes.Name;

public class fensterkomponente {
	
 public JFrame fenster;
 public static JButton buttonProf;
 public static JButton buttonStudent;
 public JPanel panel;
 public static JTextField txtfldVorname;
 public static JTextField txtfldNachname;
 public static String eingabe;
  
 public fensterkomponente(){
  fenster = new JFrame("Fenster");
  fenster.setSize(700,400);
  fenster.setLocation(300,300);
  fenster.setVisible(true);

  panel = new JPanel();
  buttonProf = new JButton("Prof");
  buttonStudent = new JButton("Student");
  txtfldVorname = new JTextField("",15);
  txtfldNachname = new JTextField("",15);
  
  panel.add(buttonProf);
  panel.add(buttonStudent);
  panel.add(txtfldVorname);
  panel.add(txtfldNachname);

  fenster.add(panel);
  fenster.setVisible(true);
  
 

  	
 }//ActionListener
 
 public static void main(String[] args){
  fensterkomponente g = new fensterkomponente();
 }


}//main


What I have tried:

I tried to show the information from the database(I use SQLWorkbench), in a textbox but I don't know how to put the code from the class in the UI class (because the action listener is there).
Posted
Comments
wseng 18-May-18 2:35am    
you should create two different pages, one for retrieve and another for insert.
Member 13814158 18-May-18 7:19am    
Yes, but how can I access to the variables between the two classes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900