gpt4 book ai didi

java - 在 Openshift 上部署 war 文件后,找不到合适的驱动程序错误

转载 作者:行者123 更新时间:2023-12-02 14:28:44 24 4
gpt4 key购买 nike

大家好,我知道 openshift 线程的找不到合适的驱动程序错误之前已经解决了,但是,after the multiple errors当我从 Eclipse 部署一个项目(一个 Maven 项目)时,我尝试在 webapp 文件夹上部署 war 文件(一个动态 Web 项目),然后只需 git add 、提交和推送,最后我让我的 servlet 运行但我不能与 mysql 数据库通信 我已将 mysql_connector 添加到 web-inf/lib/文件夹中,但仍然没有任何结果!就像我说的,我在一个动态 Web 项目中,所以我无法将依赖项添加到 pom.xml 文件有人知道吗?

我的java文件:

import java.io.*;

//import java.util.*;
//import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//krijojme nje klase qe zgjaton superclassen HttpServlet
public class DBConnection extends HttpServlet {
public static final String MYSQL_USERNAME = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
public static final String MYSQL_PASSWORD = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
public static final String MYSQL_DATABASE_HOST = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
public static final String MYSQL_DATABASE_PORT = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
public static final String MYSQL_DATABASE_NAME = "jbossews";
/**
* kjo ketu nuk e di per cfare perdoret po duhet vene
*/
private static final long serialVersionUID = 1L;

//krijojme nje metode te klases HttpServlet me emrin service
//dhe qe merr 2 parametra nje reqyest dhe nje response
//si dhe hedh disa lloje gabimesh si psh input out exeption
//si dhe servlet Exception
public void service(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
//ketu tregon si do te jete pergjigja jone ne kete rast e duam html
response.setContentType("text/html");
//dhe fillojme te paraqisim
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet JDBC</title></head>");
out.println("<body>");
out.println("<h1>Servlet JDBC</h1>");
out.println("</body></html>");
// connecting to database
//krijon ketu parametrat per tu lidhur me databasin
Connection con = null;
Statement st = null;
ResultSet rs = null;


try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException cnfe) {
out.println(cnfe);

}
String url = "mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/MYSQL_DATABASE_NAME";
con = DriverManager.getConnection(url, MYSQL_USERNAME, MYSQL_PASSWORD);
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM servlet"); //shkruan queryn
// displaying records
while(rs.next()){ //nderkohe qe ka te dhena nga query jone

out.print(rs.getObject(1).toString());
out.print("\t\t\t");
out.print(rs.getObject(2).toString());
out.print("<br>");
}
} catch (SQLException ex) {
out.println(ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}

} catch (SQLException ex) {
out.println(ex);
}
}
}

}

P.s 我还必须补充一点,我已经在本地主机上尝试了我的项目并且它有效

最佳答案

网址不应该是这样的吗:

String url = "jdbc:mysql://" + MYSQL_DATABASE_HOST + ":" + MYSQL_DATABASE_PORT +
"/" + MYSQL_DATABASE_NAME;

即协议(protocol)是 "jdbc:mysql:" 并且参数不会(?)在字符串中自动替换...

关于java - 在 Openshift 上部署 war 文件后,找不到合适的驱动程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21452716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com