我開發了一個使用jersey的REST應用程序,在那裏連接到數據庫(mysql)我使用jdbc連接。以下是配置。部署後未找到jdbc mysql驅動程序
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/DBNAME";
static final String USER = "root";
static final String PASS = "********";
public List<Item> getAll() {
List<Item> results = new ArrayList<>();
try (Connection conn = (Connection) DriverManager.getConnection(DB_URL,
USER, PASS);
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM items");) {
while (rs.next()) {
// Retrieve by column name
System.out.println(rs.getInt("ID"));
System.out.println(rs.getString("FormerCode"));
System.out.println(rs.getString("NewCode"));
results.add(new Item(rs.getInt("ID"), rs
.getString("FormerCode"), rs.getString("NewCode")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
}
當我通過Eclipse部署時,所有的東西都能正常工作。但是,當我創建war文件並在tomcat服務器上手動部署它時,它會得到異常情況,說沒有合適的驅動程序。
No suitable driver found for 'jdbc:mysql://localhost:3306/DBNAME
我已將mysql連接器jar文件包含在WEB-INF/lib文件夾和TOMCAT_HOME/lib文件夾中。但我仍然收到這個錯誤。服務器在Ubuntu Server 14.04上運行。
添加jar的路徑。
src/main/webapp/WEB-INF/lib
usr/share/tomcat7/lib
什麼似乎是問題,因此我已經將jar文件添加到必要的位置?
尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建最小,完整和可驗證示例。 –
對不起,我已經包含了代碼。 – janitha000
好的,你已經添加了代碼,但不清楚你在問什麼。你想知道爲什麼沒有加載驅動程序,或者你創建了一個不起作用的代碼?這是*不是* MCVE的Web應用程序。 –