I'm making a book buying program using Netbeans. First of all, I only do the goods part to pay for the books in the cart cart. Also, following the loading of images in JTable on YouTube and Google, it causes harm, floats, and does not work well. The main point is that when I run it, I have to load the book data in the DB, load it into the JTable, and pay. I would appreciate it if you could look at my code and give me feedback and code to add.
First of all, my DB columns are book_id , image, price, total, book_name.
Below is the DB connection code.
package yju_bookstore;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class Carrier_F_Text2 extends javax.swing.JFrame {
Connection con;
public Carrier_F_Text2() {
initComponents();
String url ="jdbc:mysql://localhost/book_db";
String id = "root";
String password = "root";
try {
con = DriverManager.getConnection(url, id, password);
} catch (Exception e) {
System.out.println("DB ERROR : " + e.getMessage());
}
}
The code below is the code that loads db data when the window is opened. But it doesn't work...
private void formWindowOpened(java.awt.event.WindowEvent evt) {
String sql = "SELECT * FROM book_db";
try {
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
DefaultTableModel model = (DefaultTableModel) jTableMain.getModel();
model.setRowCount(0);
while(rs.next()) {
model.addRow(new String[] {rs.getString(1), rs.getString(2), rs.getString(3)});
}
} catch (Exception e) {
System.out.println("formWindowOpened ERROR : " + e.getMessage());
}
}