Adding Records to MySQL Using Java GUI

Published: 14 March 2022
on channel: DR Database Channel
83
0

This video is a step by step tutorial for using Java to write data to a MySQL database using the SQL Insert statement.

The code used in the example is here:

public class BookAdder {

private JFrame frame;
public static JTextField txtbookid;
public static JTextField txtbookname;
public static JTextField txtauthorlast;

public static int bookid;
public static String bookname;
public static String authorlast;

/**
Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookAdder window = new BookAdder();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
Create the application.
*/
public BookAdder() {
initialize();
}

/**
Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 550, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblNewLabel = new JLabel("Add a Book");
lblNewLabel.setBackground(Color.LIGHT_GRAY);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Source Sans Pro", Font.PLAIN, 24));
lblNewLabel.setBounds(169, 27, 209, 41);
frame.getContentPane().add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("Book ID");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel_1.setBounds(81, 88, 98, 29);
frame.getContentPane().add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel("Book Name");
lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_2.setBounds(56, 144, 123, 24);
frame.getContentPane().add(lblNewLabel_2);

JLabel lblNewLabel_3 = new JLabel("Author Last Name");
lblNewLabel_3.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_3.setBounds(35, 191, 144, 29);
frame.getContentPane().add(lblNewLabel_3);

txtbookid = new JTextField();
txtbookid.setToolTipText("ID");
txtbookid.setFont(new Font("Tahoma", Font.PLAIN, 15));
txtbookid.setBounds(189, 91, 117, 23);
frame.getContentPane().add(txtbookid);
txtbookid.setColumns(10);


txtbookname = new JTextField();
txtbookname.setBounds(189, 144, 291, 29);
frame.getContentPane().add(txtbookname);
txtbookname.setColumns(30);

txtauthorlast = new JTextField();
txtauthorlast.setBounds(189, 196, 189, 23);
frame.getContentPane().add(txtauthorlast);
txtauthorlast.setColumns(10);

JButton btnaddbook = new JButton("Add Book");
btnaddbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {


try {BookAdder.post(); }
catch(Exception e1){
System.out.println(e1); }
}

});

btnaddbook.setFont(new Font("Tahoma", Font.PLAIN, 17));
btnaddbook.setBounds(189, 267, 117, 29);
frame.getContentPane().add(btnaddbook);
}

// Create DB Connection

public static Connection getConnection() throws Exception{
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://studentmysql.tamut.local:3306/re45";
String username = "reav1234";
String password = "password";

Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Successfully Connected to DB");
return conn;
}catch(Exception e) {System.out.println(e); System.out.println("Connection problem somewhere");}

return null;
}
// End Create Connection

//Insert a record
public static void post() throws Exception{
bookid = Integer.parseInt(txtbookid.getText()); // book ID
bookname = txtbookname.getText(); // book name
authorlast = txtauthorlast.getText(); // author last
if (txtbookname.getText().equals("")) {
JOptionPane.showMessageDialog(null,"Book name is blank - Enter a book name","Book Name error",1);}
else { // book name is not empty
if (bookname != " ") { // if the book name is not blank, try to write to
try {
Connection con = getConnection();
PreparedStatement posted = con.prepareStatement("INSERT INTO tblbook (bookid, bookname, bookauthorlastname) VALUES ("+bookid+", '"+bookname+"','"+authorlast+"')");
posted.executeUpdate();
System.out.println(bookid);
System.out.println(bookname);
System.out.println(authorlast);
} catch(Exception e) {System.out.println(e); System.out.println("errorwww");} // end try/catch
finally {System.out.println("Record Added");}
}
} // end of else
} // end insert Record

}


On this page of the site you can watch the video online Adding Records to MySQL Using Java GUI with a duration of hours minute second in good quality, which was uploaded by the user DR Database Channel 14 March 2022, share the link with friends and acquaintances, this video has already been watched 83 times on youtube and it was liked by 0 viewers. Enjoy your viewing!