我正在使用Java SWING + SQLite。当我导出可执行jar(选项Extract required libraries into generated JAR)并尝试运行时,出现错误:....>java -jar g222.jar
java.sql.SQLException: No suitable driver found for jdbc:sqlite:db/Fakturi.sqlit
e
如果我在Eclipse下运行它 - 它是有效的。这是我的代码:
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);
}
}
Connection类是:
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;
}
}
我无法理解为什么会出现此错误。
答案 0 :(得分:0)
解决:
我只想补充一下:
在任何SQL查询之前ConnectionToDb.connectDb();
。