Java数据库错误:数据库已关闭

时间:2019-01-16 21:33:10

标签: java sqlite

这是我第一次使用Java数据库。我目前在数据库方面遇到问题。当我尝试编辑数据库或向数据库添加任何内容时,出现信息对话框,提示“数据库已关闭”。

p.s:这是我第一次发布有关stackoverflow的问题,如果我搞砸了,就深表歉意。

public class Databaseconnectionfile {
    public static void main(String[] args) {
            new IDU();
        Connection c=null;
        try {
            Class.forName("org.sqlite.JDBC");
            c=DriverManager.getConnection("jdbc:sqlite:test.db");

        }
        catch(Exception e) {
                    System.out.println("Error!!");
            System.err.println(e.getClass().getName()+":"+e.getMessage());
            System.exit(0);
        }

public class IDU extends JFrame {
    JLabel JL_name,JL_father_name, JL_age, JL_id;
    JTextField JT_name, JT_father_name,JT_age, JT_id;
    JButton btn_insert, btn_update,btn_delete;

    // create the method here

    public IDU() {
        super ("Insert Update Delete App");
        JL_name=new JLabel("Name");
        JL_father_name=new JLabel("Father Name");
        JL_age=new JLabel("Age");
        JL_id=new JLabel("ID ");

        // set boudries here

        JL_id.setBounds(20,20,100,20);
        JL_name.setBounds(20,50,100,20);
        JL_father_name.setBounds(20,80,100,20);
        JL_age.setBounds(20,110,100,20);

        // for text field initilization

        JT_name =new JTextField(20);
        JT_father_name =new JTextField(20);
        JT_age =new JTextField(20);
        JT_id =new JTextField(20);

        //set boundries for the text field

        JT_id.setBounds(130,20,150,20);
        JT_name.setBounds(130,50,150,20);
        JT_father_name.setBounds(130,80,150,20);
        JT_age.setBounds(130,110,150,20);

        // adding button

        btn_insert=new JButton("Insert");
        btn_update=new JButton("Update");
        btn_delete=new JButton("Delete");

        // set boundries for button

        btn_insert.setBounds(300,50,80,20);
        btn_update.setBounds(300,80,80,20);
        btn_delete.setBounds(300,110,80,20);

        //setting the layout

        setLayout(null);
        add(JL_id);
        add(JL_name);
        add(JL_father_name);
        add(JL_age);
        add(JT_id);
        add(JT_name);
        add(JT_father_name);
        add(JT_age);
        add(btn_insert);
        add(btn_delete);
        add(btn_update);

        // implementing the action listner on the button
        btn_insert.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    theQuery("insert into users(id,name,fathername,age)values("+JT_id.getText()+","+JT_name.getText()+","+JT_father_name.getText()+","+JT_age.getText()) ;
                }catch(Exception ex) {
            }

        }



    });

        btn_update.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    theQuery("update users set name='"+JT_name.getText()+"','"+JT_father_name.getText()+","+JT_age.getText()+"where id="+JT_id.getText());
                }catch(Exception ex) {
            }

        }



    });
        btn_delete.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                try {
                    theQuery("delete from users where id="+JT_id.getText());
                }catch(Exception ex) {
            }

        }



    });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);
        setSize(500,500);


    }
        //creating the connection with database
    public void theQuery(String query) {
        Connection c=null;
        Statement stmt=null;
        try {
            c=DriverManager.getConnection("jdbc:sqlite:D:\\COMSATS\\Java Projects\\projectFinal","root","");
            stmt=c.createStatement();
            stmt.executeUpdate(query);

            JOptionPane.showMessageDialog(null, "Query Executed");

        }
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

}

0 个答案:

没有答案