Display Records From Database in a New Frame in Java:For creating this app we need the following files.
Java file
ojdbc.jar file
NetBeans IDE
SQL table
Display Records From Database in a New Frame in Java
Class No.1
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.SystemColor;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainDisplay extends JFrame {
private JPanel contentPane;
public String enter ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainDisplay frame = new MainDisplay();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainDisplay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 886, 578);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(SystemColor.controlShadow);
panel.setBounds(0, 0, 189, 539);
contentPane.add(panel);
panel.setLayout(null);
JButton ByCNmae = new JButton(“Click here”);
ByCNmae.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SearchForm sf= new SearchForm();
sf.lblStatement.setText(“Country Name”);
sf.setVisible(true);
dispose();
}
});
ByCNmae.setBounds(249, 134, 89, 54);
contentPane.add(ByCNmae);
JButton ByCCode = new JButton(“Click here”);
ByCCode.setBounds(249, 203, 89, 54);
contentPane.add(ByCCode);
JButton byCon = new JButton(“Click here”);
byCon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
byCon.setBounds(249, 274, 89, 54);
contentPane.add(byCon);
JButton byPopu = new JButton(“Click here”);
byPopu.setBounds(249, 344, 89, 54);
contentPane.add(byPopu);
JLabel lblNewLabel = new JLabel(“Search Any City”);
lblNewLabel.setFont(new Font(“Sitka Subheading”, Font.PLAIN, 24));
lblNewLabel.setBounds(361, 134, 251, 54);
contentPane.add(lblNewLabel);
JLabel lblAddNewCity = new JLabel(“Add new City”);
lblAddNewCity.setFont(new Font(“Sitka Subheading”, Font.PLAIN, 24));
lblAddNewCity.setBounds(361, 203, 251, 54);
contentPane.add(lblAddNewCity);
JLabel lblUpdateCountry = new JLabel(“Update Country”);
lblUpdateCountry.setFont(new Font(“Sitka Subheading”, Font.PLAIN, 24));
lblUpdateCountry.setBounds(361, 271, 251, 54);
contentPane.add(lblUpdateCountry);
JLabel lblDeleteCountry = new JLabel(“Delete Country”);
lblDeleteCountry.setFont(new Font(“Sitka Subheading”, Font.PLAIN, 24));
lblDeleteCountry.setBounds(361, 344, 251, 54);
contentPane.add(lblDeleteCountry);
}
}
Class No.2
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.BevelBorder;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import javax.swing.SwingConstants;
public class SearchForm extends JFrame {
public String enter=”abc”;
private JPanel contentPane;
private final JPanel panel = new JPanel();
private JTextField tfForSearch;
String space=” “;
public JLabel lblStatement;
/**
* @param enter the enter to set
*/
public void setEnter(String enter) {
this.enter = enter;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SearchForm frame = new SearchForm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SearchForm () {
MainDisplay md= new MainDisplay();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 822, 570);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel.setBounds(10, 150, 786, 138);
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
contentPane.add(panel);
panel.setLayout(null);
lblStatement = new JLabel(“Text”);
lblStatement.setFont(new Font(“Tahoma”, Font.PLAIN, 18));
lblStatement.setBounds(27, 46, 207, 28);
panel.add(lblStatement);
tfForSearch = new JTextField();
tfForSearch.setBounds(235, 49, 280, 28);
panel.add(tfForSearch);
tfForSearch.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 314, 786, 206);
contentPane.add(scrollPane);
JLabel Record = new JLabel(“Loading….”);
Record.setVerticalAlignment(SwingConstants.TOP);
scrollPane.setViewportView(Record);
JLabel RecordHeader = new JLabel(“Country Name”+space+”Continent”+space+”Region”+space+”Government Form”+space+”Population”);
RecordHeader.setVerticalAlignment(SwingConstants.TOP);
RecordHeader.setEnabled(false);
RecordHeader.setBackground(SystemColor.textHighlight);
scrollPane.setColumnHeaderView(RecordHeader);
JButton btnNewButton = new JButton(“Get Record”);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// for button
String code=tfForSearch.getText();
//
String url = “jdbc:mysql://localhost:3306/world”;
Connection conn = null;
try {
conn = DriverManager.getConnection(url, “AliHaider”, “lahorepakistan”);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Step-2 Create a Statement to Execute Query
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ResultSet rs = null;
try {
rs = stmt.executeQuery(“SELECT * FROM country where code = ‘” + code+”‘”);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Step-3 Process the Result of Query
try {
while(rs.next()) {
// String name = rs.getString(1);
// String population = rs.getString(2);
String CName = rs.getString(2);
String Continent = rs.getString(3);
String Region = rs.getString(4);
String Government = rs.getString(12);
String Population = rs.getString(7);
// Record.setText(“Country Name Continent Region Gornment Type Population”);
Record.setText(CName+space+Continent+space+Region+space+Government+space+Population);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Step-4 Close the Connection
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//
}
});
btnNewButton.setBounds(545, 49, 118, 28);
panel.add(btnNewButton);
}
}
Related