Hi all I know that the No Suitable Driver Found Error
for openshift thread has been solved before but , after the multiple errors i get when i deploy a project from eclipse( a maven one) i have tried to deploy the war file ( a dynamic web project) on webapp folder and then just git add , commit and push and finally i got my servlet running But i cant communicate with mysql database I have added the mysql_connector to the web-inf/lib/ folder but still nothing ! Like i said i am on a dynamic web project so i cant add the dependencies to the pom.xml file Does anyone have a idea?
My java file:
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);
}
}
}
}
Ps also i have to add that i already tried my project on localhost and it worked
Shouldn't the url be something like:
String url = "jdbc:mysql://" + MYSQL_DATABASE_HOST + ":" + MYSQL_DATABASE_PORT +
"/" + MYSQL_DATABASE_NAME;
Ie the protocol is "jdbc:mysql:"
and the parameters are not (?) substituted automatically in the string...
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.