简体   繁体   中英

Loading data from a sqlite database in Java?

I am new to the world of Java and sqlite database. I was trying out some codes about loading data from a database into a tabular view using java in eclipse. Now, here is the thing, when my data loads from the database, i want to add an extra column which contains a checkbox(unchecked). Can someone suggest me how do i implement it? Please note that when i run the code, it works absolutely fine and shows no error....I am able to see all the records in my table without any error. Just want an unchecked checkbox to be added to each row so that when i click on it, i can do something. Thats my goal..

Here is what i tried so far.. I have two classes namely:- 1. sqliteConnection.java(which is for connection ) 2. employeeinfo.java class which is for loading the data from the database into a table and other functionalities.

import java.sql.*;
import javax.swing.*;
public class sqliteConnection {

    Connection conn = null;

    public static Connection dbConnector()
    {
        try{
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\user\\Downloads\\Documents\\MyJavaProjects\\Phone\\Employee.sqlite");
            JOptionPane.showMessageDialog(null, "Connection Succesful");

            return conn;

        }

        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }

    public sqliteConnection() {
        // TODO Auto-generated constructor stub
    }

}

employeeinfo.java --

import java.awt.BorderLayout;
public class employeeinfo extends JFrame {

    private JPanel contentPane;
    private JTable table;

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

    /**
     * Create the frame.
     */
    Connection connection = null;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;

    public employeeinfo() {
        connection = sqliteConnection.dbConnector();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 842, 457);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblJframe = new JLabel("Jframe2");
        lblJframe.setBounds(47, 37, 46, 14);
        contentPane.add(lblJframe);

        JButton btnLoad = new JButton("Load");
        btnLoad.addActionListener(new ActionListener() {
            PreparedStatement pat;

            public void actionPerformed(ActionEvent arg0) {
                try {
                    String query = "select eid,Name,Surname,Age from EmployeeInfo ";
                    pat = connection.prepareStatement(query);

                    ResultSet rs = pat.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));





                }

                catch (Exception e)
                {

                }
            }
        });
        btnLoad.setBounds(104, 33, 89, 23);
        contentPane.add(btnLoad);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(429, 48, 370, 232);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        JLabel lblName = new JLabel("EID");
        lblName.setBounds(10, 98, 46, 21);
        contentPane.add(lblName);

        textField = new JTextField();
        textField.setBounds(107, 99, 126, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblName_1 = new JLabel("Name");
        lblName_1.setBounds(10, 154, 46, 14);
        contentPane.add(lblName_1);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(107, 151, 126, 20);
        contentPane.add(textField_1);

        JLabel lblSurname = new JLabel("Surname");
        lblSurname.setBounds(10, 204, 46, 14);
        contentPane.add(lblSurname);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(104, 201, 126, 20);
        contentPane.add(textField_2);

        JLabel lblAge = new JLabel("Age");
        lblAge.setBounds(10, 278, 46, 14);
        contentPane.add(lblAge);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(104, 275, 126, 20);
        contentPane.add(textField_3);

        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                PreparedStatement pat;
                try {
                    String query = "insert into EmployeeInfo(EID,name,surname,age) values(?,?,?,?)";
                    pat = connection.prepareStatement(query);

                     pat.setString(1, textField.getText());
                      pat.setString(2, textField_1.getText());
                      pat.setString(3, textField_2.getText());
                        pat.setString(4, textField_3.getText());
                        pat.execute();

                        JOptionPane.showMessageDialog(null, "Data Saved successfully");


                    pat.close();




                }

                catch (Exception e)
                {
                    e.printStackTrace();

                }
            }
        });
        btnSave.setBounds(72, 349, 89, 23);
        contentPane.add(btnSave);

        JButton btnClear = new JButton("Clear");
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
                textField_1.setText("");
                textField_2.setText("");
                textField_3.setText("");



            }
        });
        btnClear.setBounds(206, 349, 89, 23);
        contentPane.add(btnClear);

        JButton btnUpdate = new JButton("Update");
        btnUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try
                {

                    String query="Update EmployeeInfo set EID='"+textField.getText()+"',Name='"+textField_1.getText()+"'"
                            +",Surname='"+textField_2.getText()+"',Age='"+textField_3.getText()+"'where EID='"+textField.getText()+"'";
                    // JOptionPane.showMessageDialog(null, query);

                    PreparedStatement pst=connection.prepareStatement(query);


                    pst.execute();

                    JOptionPane.showMessageDialog(null, "Data Saved successfully");

                    pst.close();


                }
                catch(Exception f)
                {
                    f.printStackTrace();
                }
            }
        });
        btnUpdate.setBounds(323, 349, 89, 23);
        contentPane.add(btnUpdate);

        JButton btnDelete = new JButton("Delete");
        btnDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try
                {

                    String query="delete from EmployeeInfo where EID='"+textField.getText()+"'";
                    // JOptionPane.showMessageDialog(null, query);

                    PreparedStatement pst=connection.prepareStatement(query);


                    pst.execute();

                    JOptionPane.showMessageDialog(null, "Data deleted successfully");

                    pst.close();


                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });
        btnDelete.setBounds(453, 349, 89, 23);
        contentPane.add(btnDelete);
    }
}

My suggestion is you first go through this article , it's a very simpler way of loading data from ResultSet to DefaultTableModel . After this you have to make some small changes like, you have to add another column heading to the Vector of columnNames and add a column for check boxes. Then when you process each row from the ResultSet you need to add a Boolean value to the row Vector with a checkbox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM