简体   繁体   中英

java.sql.SQLException: No suitable driver found for jdbc:sqlite:db/Fakturi.sqlite

I'm Using Java SWING + SQLite. When I export executable jar (option Extract required libraries into generated JAR) and try to run, there is error: ....>java -jar g222.jar java.sql.SQLException: No suitable driver found for jdbc:sqlite:db/Fakturi.sqlit e

If I run it under Eclipse - it's works. Here is my code:

public class MainFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textField_6;
    private JTextField textField_7;



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

    /**
     * Create the frame.
     * @throws SQLException 
     */
    public MainFrame() throws SQLException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 643, 298);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
        panel.setBounds(10, 11, 670, 287);
        contentPane.add(panel);


        JPanel panel_2 = new JPanel();
        panel_2.setLayout(null);
        panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel_2.setBounds(196, 51, 249, 100);
        panel.add(panel_2);

        JLabel label_21 = new JLabel("\u0411\u0440\u043E\u0439:");
        label_21.setBounds(10, 40, 128, 16);
        panel_2.add(label_21);

        textField_6 = new JTextField();
        textField_6.setText("0");
        textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
        textField_6.setEditable(false);
        textField_6.setColumns(10);
        textField_6.setBackground(new Color(135, 206, 250));
        textField_6.setBounds(150, 37, 42, 22);
        panel_2.add(textField_6);

        JLabel label_22 = new JLabel("\u041E\u0431\u0449\u0430 \u0441\u0442\u043E\u0439\u043D\u043E\u0441\u0442 \u0441 \u0414\u0414\u0421:");
        label_22.setBounds(10, 70, 135, 16);
        panel_2.add(label_22);

        textField_7 = new JTextField();
        textField_7.setText("0.00 \u043B\u0432.");
        textField_7.setHorizontalAlignment(SwingConstants.RIGHT);
        textField_7.setForeground(Color.RED);
        textField_7.setFont(new Font("Tahoma", Font.BOLD, 11));
        textField_7.setEditable(false);
        textField_7.setColumns(10);
        textField_7.setBackground(new Color(255, 160, 122));
        textField_7.setBounds(150, 67, 87, 22);
        panel_2.add(textField_7);

        JLabel label_23 = new JLabel("\u041F\u0440\u043E\u0444\u043E\u0440\u043C\u0430 \u0444\u0430\u043A\u0442\u0443\u0440\u0438");
        label_23.setHorizontalAlignment(SwingConstants.CENTER);
        label_23.setForeground(Color.GRAY);
        label_23.setBounds(6, 6, 224, 14);
        panel_2.add(label_23);

        String danOsnInvoices = ConnectionToDb.stmSqlQuery("SELECT printf('%.2f', ROUND(sum(quantity*price), 2)) FROM invoices_out WHERE number = '0000000001'");
        textField_6.setText(danOsnInvoices);

    }
}

And Connection class is:

public class ConnectionToDb {

    public Connection conn = null;
    public static final String DBURL = "jdbc:sqlite:db/Fakturi.sqlite";

    public static Connection connectDb() throws ClassNotFoundException,SQLException {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection(DBURL);
        return conn;
    }

    public static String dmlSqlQuery(String value) throws SQLException, ClassNotFoundException {

            Statement stmt;
            String result = null;

                Class.forName("org.sqlite.JDBC");

                Connection c = DriverManager.getConnection(DBURL);
                c.setAutoCommit(false);

                stmt = c.createStatement();
                String sql = value;
                stmt.executeUpdate(sql);

                stmt.close();
                c.commit();
                c.close();

            return result;
        }

    public static String stmSqlQuery(String value) throws SQLException {

        String result = null;
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection con = DriverManager.getConnection(DBURL);

        Statement statement = con.createStatement();

        // Enables us to retrieve values as if querying from a table
        ResultSet rs = statement.executeQuery(value);

        if (rs.next()) {
            result = rs.getString(1); // get first column returned
        }

        rs.close();
        statement.close();
        con.close();
        return result;
    }
}

I can't understand Why this error shows.

SOLVED: I just add: ConnectionToDb.connectDb(); before any SQL queries.

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