Trying to connect my java web application to mysql database using tomcat 8 server and getting same error every time: No suitable driver found for jdbc
this is my method that is trying to connect to database
public String kreiraj() {
String g="com.mysql.jdbc.Driver";
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting database...");
String url = "jdbc:mysql://localhost:3306/fc";
String user = "root";
String password = "";
try (Connection connection = DriverManager.getConnection(url, user, password)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
return "index"; }
If I start same code as standard java application it work and it connects to database without any problems.
So I figured that the problem must be something with tomcat 8 server. I tried every possible solution there is and im still getting same error.
I tried this things:
adding mysql connector jar file to build path
adding mysql connector to WEB-INF/lib folder of my web application
adding mysql connector to tomcat 8 lib folder
tried using Class.forName("com.mysql.jdbc.Driver"); but getting classnotfound error
Nothing seems to work and im starting to lose my mind. If anyone know solution it would be great. Ty
Related
I have problem with executing a jar file that creates a derby connection.
I am using netbeans; while netbeans is open the jar is executed correctly, but when I close the netbeans then I cannot connect to the database. This gives an error that the database is not found.
Code is as follows:
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample","app","app");
Statement stmt=con.createStatement();
rs = stmt.executeQuery("select * from login");
while(rs.next())
{
username[i] = rs.getString(3);
password[i] =rs.getString(8);
i++;
}
}
catch(Exception e){System.out.println(e);}
Jar execution error is
java.sql.SQLNoonTransientConnectionException: java.net.ConnectionException : Error connecting to server localhost on port 1527 with massage Connection refused : connect
What should I do to correct the problem?
Netbeans is running Derby for you. Look under Services->Databases->Java DB (probably under Java DB). When NB starts the DB, your application connects to that instance, and it's fine.
When you shut down Netbeans, it stops the Derby server and your application won't run. So you either need to switch to an embedded Derby configuration, or run a Derby database server somewhere for your application to use.
I am using the following code to connect to a derby database in a dyamic web project but it does not work. If I use the same code in a normal java project it works can you guys help me out.
private Connection connection = null;
public DBconnection(){
createConnection();
}
public void createConnection(){
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
this.connection = DriverManager.getConnection("jdbc:derby://localhost:1527/student");
}catch (Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.connection;
}
Note:
I have included all the derby libraries in my dynamic web project and also I have I have started the derby server everything works but this code does not connect to the database. Also I am running the dynamic project on a tomcat server the port for that is 8080 and the port for the derby server is 1527.
Please copy the derbybyclient.jar file which is present in derby installation lib folder into the lib folder of your tomcat installation.
Rest of the code is fine.
For more information you can refer this link
I am currently trying to contact a MySQL server.
My goal is to have a servlet that will show some table data when run.
My servelt is running on glassfish 3.1.
Here is the simple doGet method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("<h1>select * from userName:<h1>");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/people", "root", "r00tpw");
System.out.println("Connected");
PreparedStatement statement = (PreparedStatement) conn.prepareStatement("select * from userName");
ResultSet result = statement.executeQuery();
while(result.next()) {
out.print(result.getString(0));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
I have got the correct JDBC driver. The problem I have is with my source code finding it. I have tried right clicking JRE System Library->Build Path->Configure Build Path and adding an external JAR. Rather than adding the JAR to Referenced Libraries it seems to be added outside of any library scope. This means that Class.forName("com.mysql.jdbc.Driver"); is always throwing a ClassNotFoundException.
I have also tried adding the JAR to my Run Configurations->Glassfish 3.1 at [pcname]->Classpath->User Entries with no luck.
I have also tried using MS SQL Server and their JDBC driver. The same thing happens.
I have attached an image of my project structure to help.
Put the mysql-connector-java-5.1.26 to your WebContent--->WEB-INF-->lib.inside lib folder.
First of all after you install jdbc driver you need to configure it in glassfish. Create a JDBC resource and JDBC connection pool.
PS Did you add the jdbc JAR to glassfish server folder?
For glassfish too see your connector it must reside in \Glassfish3\domains\domain1\lib\databases
May be you are not added mysql driver to a server folder. Therefore, did you check your glassfish-resources.xml or JDBC connection in Glassfish admin console?
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 3 years ago.
I am working on a web application where I am creating MSSQLSERVER 2008
database dynamically.
But it's giving me
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=master
My code is:
String dbName = "db1";
try {
String url = "jdbc:sqlserver://localhost:1433;databaseName=master";
Connection connection = DriverManager.getConnection(
url,
"sa",
"roshan14121987");
Statement statement = connection.createStatement();
String sqlquery = "CREATE Database \"" + dbName + "\"; ";
statement.executeUpdate(sqlquery);
statement.close();
connection.close();
} catch(Exception e) {
e.printStackTrace();
}
I have added sqljdbc4.jar in lib. I have tried it on both NetBeans (with GlassFish and Tomcat Server) and Eclipse IDE(Tomcat Server).
On the other hand I have tried this with simple desktop application, it's working fine on both IDEs. With sqljdbc4.jar added in lib.
Before calling DriverManager.getConnection() you will have to load the SQLServer JDBC driver. You can do Class.forName("xxxx"); where xxxx is the appropriate driver class (fully qualified name with package prefix).
EDIT: Do this Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); to load the driver. Refer MSDN link for more.
Is sqljdbc4.jar in your war file under WEB-INF/lib. This is the alternate location to $CATALINA_HOME/lib and recommended to keep your web application self-contained. Further, if you should ever need to change servers, you need only drop the war into your webapps directory.
You might also need a Class.forName([database class]).newInstance, but that isn't necessary using a JDBC 4.0 driver. Good luck and if you have further problems, do leave a comment.
The error happen in the runtime on Tomcat Server,right? I think you need to make sure your sqljdbc4.jar deploy the in the Server.
I got a access database, and I'm programming in Java.
What can i use to connect my netbeans with my database on localhost?
Only found this code(it uses local db file) for Windows:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
"(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb";
Connection con = DriverManager.getConnection(url);
System.out.println("Connected!");
con.close();
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+cE.toString());
}
Maybe someone know how to modify it for mac environment?
Take a look at using the UCanAccess JDBC driver available to download. Include all the jar files in the libraries of the project and you should be able to make a connection to your access database without having Access installed. UCanAccess works best with NetBeans.
In Access Database Manipulation via JDBC is explained step by step.