JDBC Chapter 4 Lab (All in One) (1)
JDBC Chapter 4 Lab (All in One) (1)
/*
*/
package tryjdbc1;
import java.sql.*;
import javax.swing.JOptionPane;
/**
* @author deebi
*/
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//return conn;
// JOptionPane.showMessageDialog(null, ex);
return null;
conn();
Main Class
/*
*/
package tryjdbc1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
* @author deebi
*/
//Components
PreparedStatement pst;
ResultSet rs;
public Tryjdbc1() {
loadUSer();
fetch();
f.setLayout(new BorderLayout());
top.setLayout(new FlowLayout());
center.setLayout(new FlowLayout());
bottom.setLayout(new FlowLayout());
table.setFillsViewportHeight(true);
f.add(top, BorderLayout.NORTH);
f.add(center, BorderLayout.CENTER);
f.add(bottom, BorderLayout.SOUTH);
top.add(searchInfo);
top.add(searchBtn);
center.add(fullNameLabel);
center.add(fullName);
center.add(userNameLabel);
center.add(userName);
center.add(passwordLabel);
center.add(password);
center.add(ageLabel);
center.add(age);
center.add(scrollPane);
bottom.add(submitBtn);
bottom.add(showBtn);
bottom.add(updateBtn);
bottom.add(deletBtn);
bottom.add(clearBtn);
submitBtn.addActionListener(new ActionListener() {
@Override
insertData();
});
updateBtn.addActionListener(new ActionListener() {
@Override
updateData();
});
deletBtn.addActionListener(new ActionListener() {
@Override
deleteData();
});
searchBtn.addActionListener(new ActionListener() {
@Override
try {
pst = GetConnection.conn().prepareStatement(sql);
pst.setString(1, selectedItem);
rs = pst.executeQuery();
if (rs.next() == true) {
fullName.setText(rs.getString(2));
userName.setText(rs.getString(3));
password.setText(rs.getString(4));
age.setText(rs.getString(5));
ex.printStackTrace();
});
f.setSize(320, 300);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
pst = GetConnection.conn().prepareStatement(loadU);
rs = pst.executeQuery();
searchInfo.removeAllItems();
while(rs.next()) {
searchInfo.addItem(rs.getString(1));
ex.printStackTrace();
try {
int count;
pst = GetConnection.conn().prepareStatement(sql);
rs = pst.executeQuery();
count = rsm.getColumnCount();
while (rs.next()) {
table.setModel(df);
try {
pst = GetConnection.conn().prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, uname);
pst.setString(3, pass);
pst.setInt(4, ageParse);
int r = pst.executeUpdate();
if(r==1)
fullName.setText("");
userName.setText("");
password.setText("");
age.setText("");
fullName.requestFocus();
fetch();
loadUSer();
else
ex.printStackTrace();
try {
pst = GetConnection.conn().prepareStatement(sql);
pst.setString(1, updateName);
pst.setString(2, updateUsername);
pst.setString(3, updatePass);
pst.setString(4, ageStrUpdate);
pst.setString(5, selectedItem);
int r = pst.executeUpdate();
if(r == 1){
fullName.setText("");
userName.setText("");
password.setText("");
age.setText("");
fullName.requestFocus(); //focus
fetch();
else{
}
} catch (Exception ex) {
ex.printStackTrace();
try {
pst = GetConnection.conn().prepareStatement(sql);
pst.setString(1, selectedItem);
int r = pst.executeUpdate();
if(r == 1)
fullName.setText("");
userName.setText("");
password.setText("");
age.setText("");
fullName.requestFocus(); //focus
fetch();
}
else
ex.printStackTrace();
/**
*/
new Tryjdbc1();