Search Option - comboBox - java swing
DBConnection.java >> xem cách kết nối database
http://giai-ma.blogspot.com/2016/04/login-swing-eclipse-in-java-gui.html
Manager_Login.java
package Admin;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import Connect.DBConnection;
public class Manager_Login {
public JFrame frame;
private JTextField txtID;
private JTextField txtUsername;
private JTable table;
private JPasswordField txtPassword;
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
private JTextField txtSearch;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Manager_Login window = new Manager_Login();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Manager_Login() {
initialize();
try {
conn = DBConnection.getConnection();
} catch (Exception e) {
// TODO: handle exception
}
showTable();
this.frame.setResizable(false);
}
public void showTable() {
Vector cols = new Vector();
cols.addElement("ID");
cols.addElement("Username");
cols.addElement("Password");
cols.addElement("Roles");
// Data
Vector data = new Vector();
String sql = "SELECT * FROM manager.login";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Vector login = new Vector();
login.addElement(rs.getString("id"));
login.addElement(rs.getString("user"));
login.addElement(rs.getString("pass"));
login.addElement(rs.getString("roles"));
data.add(login);
}
} catch (Exception e) {
// TODO: handle exception
}
table.setModel(new DefaultTableModel(data, cols));
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 404, 428);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txtID = new JTextField();
txtID.setBounds(123, 54, 86, 20);
frame.getContentPane().add(txtID);
txtID.setColumns(10);
txtUsername = new JTextField();
txtUsername.setBounds(123, 85, 86, 20);
frame.getContentPane().add(txtUsername);
txtUsername.setColumns(10);
txtPassword = new JPasswordField();
txtPassword.setBounds(123, 112, 86, 20);
frame.getContentPane().add(txtPassword);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "Admin", "Acountant", "Manager", "Saler" }));
comboBox.setBounds(123, 143, 86, 20);
frame.getContentPane().add(comboBox);
JLabel lblId = new JLabel("ID:");
lblId.setBounds(36, 57, 61, 14);
frame.getContentPane().add(lblId);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(36, 88, 77, 14);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(36, 115, 77, 14);
frame.getContentPane().add(lblPassword);
JLabel lblRoles = new JLabel("Roles:");
lblRoles.setBounds(36, 146, 61, 14);
frame.getContentPane().add(lblRoles);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sql = "INSERT INTO manager.login (id,user, pass, roles) VALUES (?,?,?,?)";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, Integer.parseInt(txtID.getText()));
stmt.setString(2, txtUsername.getText());
stmt.setString(3, txtPassword.getText());
stmt.setString(4, comboBox.getSelectedItem().toString());
stmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
showTable();
}
});
btnAdd.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\add-icon.png"));
btnAdd.setBounds(234, 53, 106, 23);
frame.getContentPane().add(btnAdd);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sql = "UPDATE manager.login SET user=?, pass=?, roles=? WHERE id =?";
int id = Integer.parseInt(txtID.getText());
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, txtUsername.getText());
stmt.setString(2, txtPassword.getText());
stmt.setString(3, comboBox.getSelectedItem().toString());
stmt.setInt(4, id);
stmt.executeUpdate();
} catch (Exception e2) {
// TODO: handle exception
}
showTable();
}
});
btnUpdate.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\Actions-edit-redo-icon.png"));
btnUpdate.setBounds(234, 84, 106, 23);
frame.getContentPane().add(btnUpdate);
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sql = "DELETE FROM manager.login WHERE id =?";
int id = Integer.parseInt(txtID.getText());
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (Exception e2) {
// TODO: handle exception
}
showTable();
}
});
btnDelete.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\Actions-edit-delete-icon.png"));
btnDelete.setBounds(234, 111, 106, 23);
frame.getContentPane().add(btnDelete);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 185, 346, 146);
frame.getContentPane().add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int row = table.getSelectedRow();
if (row != -1) {
txtID.setText(table.getValueAt(row, 0).toString());
txtUsername.setText(table.getValueAt(row, 1).toString());
txtPassword.setText(table.getValueAt(row, 2).toString());
comboBox.setSelectedItem(table.getValueAt(row, 3).toString());
}
}
});
scrollPane.setViewportView(table);
table.setModel(
new DefaultTableModel(new Object[][] {}, new String[] { "ID", "Username", "Password", "Roles" }));
JLabel lblXinChao = new JLabel("Xin chao Admin");
lblXinChao.setBounds(36, 11, 95, 14);
frame.getContentPane().add(lblXinChao);
JLabel lblManagerLogin = new JLabel("Manager Login");
lblManagerLogin.setFont(new Font("MV Boli", Font.PLAIN, 16));
lblManagerLogin.setBounds(216, 0, 124, 32);
frame.getContentPane().add(lblManagerLogin);
txtSearch = new JTextField();
txtSearch.setBounds(116, 354, 144, 20);
frame.getContentPane().add(txtSearch);
txtSearch.setColumns(10);
JComboBox comboBoxOption = new JComboBox();
comboBoxOption.setModel(new DefaultComboBoxModel(new String[] { "Serch All", "Username", "UserType" }));
comboBoxOption.setBounds(20, 353, 86, 20);
frame.getContentPane().add(comboBoxOption);
JButton btnSearch = new JButton("Search");
btnSearch.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Search-icon.png"));
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Search = txtSearch.getText().trim();
Vector cols = new Vector();
cols.addElement("ID");
cols.addElement("User");
cols.addElement("Pass");
cols.addElement("Roles");
// Data
Vector dataSearch = new Vector();
String option = comboBoxOption.getSelectedItem().toString();
switch (option) {
case "Search All":
try {
String sql = "SELECT * FROM manager.login";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Vector user = new Vector();
user.add(rs.getInt("id"));
user.add(rs.getString("user"));
user.add(rs.getString("pass"));
user.add(rs.getString("roles"));
if (user.toString().toLowerCase().contains(Search)) {
dataSearch.addElement(user);
}
}
} catch (Exception e2) {
// TODO: handle exception
}
break;
case "Username":
try {
String sqlUser = "SELECT * FROM manager.login WHERE user LIKE ?";
stmt = conn.prepareStatement(sqlUser);
stmt.setString(1, txtSearch.getText()+"%");
rs = stmt.executeQuery();
while (rs.next()) {
Vector user = new Vector();
user.add(rs.getInt("id"));
user.add(rs.getString("user"));
user.add(rs.getString("pass"));
user.add(rs.getString("roles"));
dataSearch.addElement(user);
}
} catch (Exception e2) {
// TODO: handle exception
}
break;
case "UserType":
try {
String sqlType = "SELECT * FROM manager.login WHERE roles LIKE ?";
stmt = conn.prepareStatement(sqlType);
stmt.setString(1, txtSearch.getText()+"%");
rs = stmt.executeQuery();
while (rs.next()) {
Vector user = new Vector();
user.add(rs.getInt("id"));
user.add(rs.getString("user"));
user.add(rs.getString("pass"));
user.add(rs.getString("roles"));
dataSearch.addElement(user);
}
} catch (Exception e2) {
// TODO: handle exception
}
break;
default:
try {
String sqlDef = "SELECT * FROM manager.login";
stmt = conn.prepareStatement(sqlDef);
rs = stmt.executeQuery();
while (rs.next()) {
Vector user = new Vector();
user.add(rs.getInt("id"));
user.add(rs.getString("user"));
user.add(rs.getString("pass"));
user.add(rs.getString("roles"));
if (user.toString().toLowerCase().contains(Search)) {
dataSearch.addElement(user);
}
}
} catch (Exception e2) {
// TODO: handle exception
}
break;
}
table.setModel(new DefaultTableModel(dataSearch, cols));
}
});
btnSearch.setBounds(270, 354, 95, 20);
frame.getContentPane().add(btnSearch);
JButton btnNewButton = new JButton("About");
btnNewButton
.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Actions-help-about-icon.png"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Note note = new Note();
note.frame.setVisible(true);
note.frame.setResizable(false);
}
});
btnNewButton.setBounds(234, 142, 106, 23);
frame.getContentPane().add(btnNewButton);
}
}
Search smart
Display welcome admin!
Create >> User.java << in package Modle
package Model; public class User { private int id; private String User; private String Pass; private String Roles; public User(int id, String user, String pass, String roles) { super(); this.id = id; User = user; Pass = pass; Roles = roles; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser() { return User; } public void setUser(String user) { User = user; } public String getPass() { return Pass; } public void setPass(String pass) { Pass = pass; } public String getRoles() { return Roles; } public void setRoles(String roles) { Roles = roles; } public User() { super(); // TODO Auto-generated constructor stub } }
Login.java << Change
package Login; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import Admin.Manager_Login; import Connect.DBConnection; import Model.User; public class Login { private JFrame frame; private JTextField txtUser; private Connection conn; private PreparedStatement stmt; private ResultSet rs; private JPasswordField txtPass; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Login window = new Login(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Login() { initializeLogin(); try { conn = DBConnection.getConnection(); } catch (Exception e) { // TODO: handle exception } this.frame.setResizable(false); } /** * Initialize the contents of the frame. */ private void initializeLogin() { frame = new JFrame(); frame.setBounds(100, 100, 387, 305); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JLabel lblLogin = new JLabel("Login"); lblLogin.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\user-login-icon.png")); lblLogin.setFont(new Font("Simplified Arabic Fixed", Font.BOLD, 37)); lblLogin.setBounds(93, 11, 207, 66); frame.getContentPane().add(lblLogin); txtUser = new JTextField(); txtUser.setBounds(162, 103, 133, 20); frame.getContentPane().add(txtUser); txtUser.setColumns(10); JButton btnLogin = new JButton("Login"); btnLogin.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\About-me-icon.png")); btnLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String User = txtUser.getText(); String Pass = txtPass.getText(); String sql = "SELECT * FROM manager.login WHERE user=? and pass=?"; try { stmt = conn.prepareStatement(sql); stmt.setString(1, txtUser.getText()); stmt.setString(2, txtPass.getText()); rs = stmt.executeQuery(); if (rs.next()) { User userHello = new User(); userHello.setUser(rs.getString("user")); String user = rs.getString("roles"); switch (user) { case "Admin": Manager_Login window = new Manager_Login(userHello); frame.dispose(); window.frame.setVisible(true); break; case "Saler": JOptionPane.showMessageDialog(null, "Xin loi ban la Saler khong phai admin"); break; case "Acountant": JOptionPane.showMessageDialog(null, "Xin loi ban la Acountant khong phai admin"); break; case "Manager": JOptionPane.showMessageDialog(null, "Xin loi ban la Manager khong phai admin"); break; default: break; } } else { JOptionPane.showMessageDialog(null, "User or Pass khong dung!"); } } catch (Exception e2) { // TODO: handle exception } } }); btnLogin.setBounds(75, 186, 89, 23); frame.getContentPane().add(btnLogin); JButton btnExit = new JButton("Exit"); btnExit.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Actions-window-close-icon.png")); btnExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btnExit.setBounds(207, 186, 89, 23); frame.getContentPane().add(btnExit); JLabel lblUsername = new JLabel("Username:"); lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11)); lblUsername.setBounds(60, 106, 74, 14); frame.getContentPane().add(lblUsername); JLabel lblPassword = new JLabel("Password:"); lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11)); lblPassword.setBounds(60, 147, 74, 14); frame.getContentPane().add(lblPassword); txtPass = new JPasswordField(); txtPass.setBounds(162, 144, 133, 20); frame.getContentPane().add(txtPass); } }
Manager_Login.java << add
package Admin; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Vector; import javax.swing.DefaultComboBoxModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import Connect.DBConnection; import Model.User; public class Manager_Login { public JFrame frame; private JTextField txtID; private JTextField txtUsername; private JTable table; private JPasswordField txtPassword; private Connection conn; private PreparedStatement stmt; private ResultSet rs; private JTextField txtSearch; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Manager_Login window = new Manager_Login(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Manager_Login() { initialize(); try { conn = DBConnection.getConnection(); } catch (Exception e) { // TODO: handle exception } showTable(); this.frame.setResizable(false); } public Manager_Login(User userHello) { initialize(); try { conn = DBConnection.getConnection(); } catch (Exception e) { // TODO: handle exception } JLabel lblHello = new JLabel(""); lblHello.setBounds(100, 11, 76, 14); frame.getContentPane().add(lblHello); lblHello.setText(userHello.getUser()); showTable(); this.frame.setResizable(false); } public void showTable() { Vector cols = new Vector(); cols.addElement("ID"); cols.addElement("Username"); cols.addElement("Password"); cols.addElement("Roles"); // Data Vector data = new Vector(); String sql = "SELECT * FROM manager.login"; try { stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { Vector login = new Vector(); login.addElement(rs.getString("id")); login.addElement(rs.getString("user")); login.addElement(rs.getString("pass")); login.addElement(rs.getString("roles")); data.add(login); } } catch (Exception e) { // TODO: handle exception } table.setModel(new DefaultTableModel(data, cols)); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 404, 428); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); txtID = new JTextField(); txtID.setBounds(123, 54, 86, 20); frame.getContentPane().add(txtID); txtID.setColumns(10); txtUsername = new JTextField(); txtUsername.setBounds(123, 85, 86, 20); frame.getContentPane().add(txtUsername); txtUsername.setColumns(10); txtPassword = new JPasswordField(); txtPassword.setBounds(123, 112, 86, 20); frame.getContentPane().add(txtPassword); JComboBox comboBox = new JComboBox(); comboBox.setModel(new DefaultComboBoxModel(new String[] { "Admin", "Acountant", "Manager", "Saler" })); comboBox.setBounds(123, 143, 86, 20); frame.getContentPane().add(comboBox); JLabel lblId = new JLabel("ID:"); lblId.setBounds(36, 57, 61, 14); frame.getContentPane().add(lblId); JLabel lblUsername = new JLabel("Username:"); lblUsername.setBounds(36, 88, 77, 14); frame.getContentPane().add(lblUsername); JLabel lblPassword = new JLabel("Password:"); lblPassword.setBounds(36, 115, 77, 14); frame.getContentPane().add(lblPassword); JLabel lblRoles = new JLabel("Roles:"); lblRoles.setBounds(36, 146, 61, 14); frame.getContentPane().add(lblRoles); JButton btnAdd = new JButton("Add"); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String sql = "INSERT INTO manager.login (id,user, pass, roles) VALUES (?,?,?,?)"; try { stmt = conn.prepareStatement(sql); stmt.setInt(1, Integer.parseInt(txtID.getText())); stmt.setString(2, txtUsername.getText()); stmt.setString(3, txtPassword.getText()); stmt.setString(4, comboBox.getSelectedItem().toString()); stmt.executeUpdate(); } catch (Exception e) { // TODO: handle exception } showTable(); } }); btnAdd.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\add-icon.png")); btnAdd.setBounds(234, 53, 106, 23); frame.getContentPane().add(btnAdd); JButton btnUpdate = new JButton("Update"); btnUpdate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String sql = "UPDATE manager.login SET user=?, pass=?, roles=? WHERE id =?"; int id = Integer.parseInt(txtID.getText()); try { stmt = conn.prepareStatement(sql); stmt.setString(1, txtUsername.getText()); stmt.setString(2, txtPassword.getText()); stmt.setString(3, comboBox.getSelectedItem().toString()); stmt.setInt(4, id); stmt.executeUpdate(); } catch (Exception e2) { // TODO: handle exception } showTable(); } }); btnUpdate.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\Actions-edit-redo-icon.png")); btnUpdate.setBounds(234, 84, 106, 23); frame.getContentPane().add(btnUpdate); JButton btnDelete = new JButton("Delete"); btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String sql = "DELETE FROM manager.login WHERE id =?"; int id = Integer.parseInt(txtID.getText()); try { stmt = conn.prepareStatement(sql); stmt.setInt(1, id); stmt.executeUpdate(); } catch (Exception e2) { // TODO: handle exception } showTable(); } }); btnDelete.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\Actions-edit-delete-icon.png")); btnDelete.setBounds(234, 111, 106, 23); frame.getContentPane().add(btnDelete); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(20, 185, 346, 146); frame.getContentPane().add(scrollPane); table = new JTable(); table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { int row = table.getSelectedRow(); if (row != -1) { txtID.setText(table.getValueAt(row, 0).toString()); txtUsername.setText(table.getValueAt(row, 1).toString()); txtPassword.setText(table.getValueAt(row, 2).toString()); comboBox.setSelectedItem(table.getValueAt(row, 3).toString()); } } }); scrollPane.setViewportView(table); table.setModel( new DefaultTableModel(new Object[][] {}, new String[] { "ID", "Username", "Password", "Roles" })); JLabel lblXinChao = new JLabel("Xin chao"); lblXinChao.setBounds(36, 11, 50, 14); frame.getContentPane().add(lblXinChao); JLabel lblManagerLogin = new JLabel("Manager Login"); lblManagerLogin.setFont(new Font("MV Boli", Font.PLAIN, 16)); lblManagerLogin.setBounds(216, 0, 124, 32); frame.getContentPane().add(lblManagerLogin); txtSearch = new JTextField(); txtSearch.setBounds(116, 354, 144, 20); frame.getContentPane().add(txtSearch); txtSearch.setColumns(10); JComboBox comboBoxOption = new JComboBox(); comboBoxOption.setModel(new DefaultComboBoxModel(new String[] { "Serch All", "Username", "UserType" })); comboBoxOption.setBounds(20, 353, 86, 20); frame.getContentPane().add(comboBoxOption); JButton btnSearch = new JButton("Search"); btnSearch.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Search-icon.png")); btnSearch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String Search = txtSearch.getText().trim(); Vector cols = new Vector(); cols.addElement("ID"); cols.addElement("User"); cols.addElement("Pass"); cols.addElement("Roles"); // Data Vector dataSearch = new Vector(); String option = comboBoxOption.getSelectedItem().toString(); switch (option) { case "Search All": try { String sql = "SELECT * FROM manager.login"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { Vector user = new Vector(); user.add(rs.getInt("id")); user.add(rs.getString("user")); user.add(rs.getString("pass")); user.add(rs.getString("roles")); if (user.toString().toLowerCase().contains(Search)) { dataSearch.addElement(user); } } } catch (Exception e2) { // TODO: handle exception } break; case "Username": try { String sqlUser = "SELECT * FROM manager.login WHERE user LIKE ?"; stmt = conn.prepareStatement(sqlUser); stmt.setString(1, txtSearch.getText()+"%"); rs = stmt.executeQuery(); while (rs.next()) { Vector user = new Vector(); user.add(rs.getInt("id")); user.add(rs.getString("user")); user.add(rs.getString("pass")); user.add(rs.getString("roles")); dataSearch.addElement(user); } } catch (Exception e2) { // TODO: handle exception } break; case "UserType": try { String sqlType = "SELECT * FROM manager.login WHERE roles LIKE ?"; stmt = conn.prepareStatement(sqlType); stmt.setString(1, txtSearch.getText()+"%"); rs = stmt.executeQuery(); while (rs.next()) { Vector user = new Vector(); user.add(rs.getInt("id")); user.add(rs.getString("user")); user.add(rs.getString("pass")); user.add(rs.getString("roles")); dataSearch.addElement(user); } } catch (Exception e2) { // TODO: handle exception } break; default: try { String sqlDef = "SELECT * FROM manager.login"; stmt = conn.prepareStatement(sqlDef); rs = stmt.executeQuery(); while (rs.next()) { Vector user = new Vector(); user.add(rs.getInt("id")); user.add(rs.getString("user")); user.add(rs.getString("pass")); user.add(rs.getString("roles")); if (user.toString().toLowerCase().contains(Search)) { dataSearch.addElement(user); } } } catch (Exception e2) { // TODO: handle exception } break; } table.setModel(new DefaultTableModel(dataSearch, cols)); } }); btnSearch.setBounds(270, 354, 95, 20); frame.getContentPane().add(btnSearch); JButton btnNewButton = new JButton("About"); btnNewButton .setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Actions-help-about-icon.png")); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Note note = new Note(); note.frame.setVisible(true); note.frame.setResizable(false); } }); btnNewButton.setBounds(234, 142, 106, 23); frame.getContentPane().add(btnNewButton); } }
Back link to example >>
Download:import https://drive.google.com/open?id=0BzClcKmnT6YbbjZFOUhobU05aGM
Download import Database https://drive.google.com/open?id=0BzClcKmnT6YbV1RyVERGVDdZekE
Download:RunDemoJAR https://drive.google.com/open?id=0BzClcKmnT6YbbW1LUllPMEp6TjQ
Back >> http://giai-ma.blogspot.com/2016/04/login-swing-eclipse-in-java-gui.html
0 nhận xét:
Post a Comment