AdvSushant
AdvSushant
/**
* Create the frame.
*/
public swingdata() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 937, 645);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Book name");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
lblNewLabel.setBounds(86, 50, 156, 36);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Book Author Name");
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
lblNewLabel_1.setBounds(86, 107, 213, 36);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Book\t ID");
lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
lblNewLabel_2.setBounds(86, 149, 156, 42);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("todays date");
lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
lblNewLabel_3.setBounds(62, 201, 213, 42);
contentPane.add(lblNewLabel_3);
textField = new JTextField();
textField.setBounds(278, 50, 551, 31);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(284, 107, 544, 32);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(284, 153, 544, 31);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
}
});
textField_3.setBounds(284, 206, 538, 31);
contentPane.add(textField_3);
textField_3.setColumns(10);
JButton btnNewButton = new JButton("Submit and exit");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get current date
LocalDate currentDate = LocalDate.now();
// Format date (optional)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-
dd");
String id1=textField.getText();
String id2=textField_1.getText();
String id3=textField_2.getText();
String id4=textField_4.getText();
String s2 = currentDate.format(formatter);
System.out.println(id1+"\n"+id2+"\n"+id3+"\n");
try {
theQuery("insert into
libarary(book_name,Author_name,id,date_of_book,today_date) values(' "
+id1+" ' , ' "
+id2+" ' , ' "
+id3+" ' , ' "
+ s2+" ' , ' "+ id4+" ' )" );
}catch(Exception e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
Connection conn = null;
Statement stmt = null;
public void theQuery(String query) {
try {
conn = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root123");
stmt=conn.createStatement();
stmt.executeUpdate(query);
JOptionPane.showMessageDialog( null,"Done Successfully");
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
btnNewButton.setBounds(86, 345, 230, 60);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Clear");
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
btnNewButton_1.setBounds(410, 345, 165, 60);
contentPane.add(btnNewButton_1);
JLabel lblNewLabel_4 = new JLabel("Date of Books Add");
lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
lblNewLabel_4.setBounds(62, 251, 201, 31);
contentPane.add(lblNewLabel_4);
OUTPUT