Cześć wszystkim
Mam problem z aplikacja, wszystko działa tak jak powinno tylko problem polega na tym, chciałbym aby można było zrobić update tylko po nazwie nie wpisując wszystkie pola, oraz jak nie wpisze nic w żadne pola to żeby pojawiła się informacja nie dokonano żadnego wpisu lub coś podobnego ale nie wiem jak. Czy zastosować array ?, był by może ktoś w stanie wyjaśnić jak albo wkleić kod przykładowy abym mógł się odnieść.
Dziękuję z góry.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import net.proteanit.sql.DbUtils;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;
public class main extends javax.swing.JFrame {
Connection con =null;
ResultSet rs=null;
PreparedStatement pts=null;
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTable DB;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
main window = new main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public main() {
initialize();
connection();
fetch();
}
/*Metoda łączenie */
public void connection(){
Statement st = null;
try{
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/bookshop","root","Password");
st = con.createStatement();
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
/* Metoda to pobierania danych z sql*/
public void fetch(){
try{
String q="select * from book";
pts=con.prepareStatement(q);
rs=pts.executeQuery();
DB.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
/*Metoda używana do przycisków : Insert,Update,Delete */
public void theQuery(String query){
Connection con = null;
Statement st = null;
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/bookshop","root","Password");
st = con.createStatement();
st.executeUpdate(query);
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
/**
* Tworzenie ramy i całego layoutu
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 768, 522);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
/*Przycisk insert + metoda*/
JButton btnNewButton = new JButton("Insert");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try(ResultSet rs = pts.executeQuery())
{
theQuery("insert into book (bookname,authorname,price) values('"+textField_1.getText()+"','"+textField_2.getText()+"',"+textField_3.getText()+")");
JOptionPane.showMessageDialog(null, "Book inserted successfully");
}
catch(Exception ex){}
}
});
btnNewButton.setBounds(22, 160, 89, 23);
frame.getContentPane().add(btnNewButton);
/*Przycisk usuń + metoda */
JButton btnNewButton_1 = new JButton("Delete");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
theQuery("delete from book where bookID = "+textField.getText());
JOptionPane.showMessageDialog(null, "Book deleted");
}
catch(Exception ex){}
}
});
btnNewButton_1.setBounds(22, 228, 89, 23);
frame.getContentPane().add(btnNewButton_1);
/* Przycisk update */
JButton btnNewButton_2 = new JButton("Update");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
theQuery("update book set bookName = '"+textField_1.getText()+"',authorName = '"+textField_2.getText()+"', price = "+textField_3.getText()+" where bookID = "+textField.getText());
if(theQuery(paramString() != null) {
JOptionPane.showMessageDialog(null, "No data was entered");
}
else{
JOptionPane.showMessageDialog(null, "Price update Successfully");
}
}
catch(Exception ex){}
}
});
btnNewButton_2.setBounds(22, 194, 89, 23);
frame.getContentPane().add(btnNewButton_2);
textField = new JTextField();
textField.setBounds(133, 35, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(133, 64, 86, 20);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(133, 95, 86, 20);
frame.getContentPane().add(textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(133, 126, 86, 20);
frame.getContentPane().add(textField_3);
JLabel lblNewLabel = new JLabel("Book ID");
lblNewLabel.setBounds(22, 35, 73, 20);
frame.getContentPane().add(lblNewLabel);
JLabel lblBookName = new JLabel("Book Name");
lblBookName.setBounds(22, 67, 73, 20);
frame.getContentPane().add(lblBookName);
JLabel lblBookAuthor = new JLabel("Book Author");
lblBookAuthor.setBounds(22, 98, 73, 20);
frame.getContentPane().add(lblBookAuthor);
JLabel lblPrice = new JLabel("Price");
lblPrice.setBounds(22, 129, 73, 20);
frame.getContentPane().add(lblPrice);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(241, 38, 478, 405);
frame.getContentPane().add(scrollPane);
DB = new JTable();
scrollPane.setViewportView(DB);
/*Przycisk Odswież*/
JButton btnRefresh = new JButton("Refresh");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fetch();
}
});
btnRefresh.setBounds(22, 262, 89, 23);
frame.getContentPane().add(btnRefresh);
}
}