Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package learning_jdbc;
- import java.sql.*;
- import java.util.*;
- import javax.swing.*;
- public class Accounts extends JFrame {
- private Connection connection;
- private void buildGUI() {
- //Do Account List
- DefaultListmodel model = new DefaultListModel();
- try (Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery("SELECT acc_id FROM acc");) {
- while(rs.next()) {
- model.addElement(rs.getString("acc_id"));
- }
- rs.close();
- } catch(SQLException e) {
- displaySQLErrors(e);
- }
- }
- public void connectToDB() {
- try {
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounts","root","thejoker");
- } catch(SQLException e) {
- System.out.println("Unable to connect to database");
- System.exit(1);
- }
- }
- private void displaySQLErrors(SQLException e) {
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- public Accounts() {
- initComponents();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (Exception e) {
- System.err.println("Unable to find and load driver");
- System.exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement