Can't add JDBC driver to Library scope - java

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?

Related

SQLException even after loading JDBC driver correctly

I'm new to java and I am using Visual Studio Code for making a Java project. I'm trying to write SQL queries after loading driver in Visual Studio Code, but I'm repeatedly getting SQLException. Here is my project folder:
src
-com/folder
-db
-DBConnection.java
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(path, username, password);
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
return false;
}
Every time I run, I get the following error
Exception in thread "main" java.sql.SQLException: No suitable driver
found for jdbc:mysql:localhost:3306/assign
I'm getting correct results when I run the same program in the src folder.
The most common reason for this type of error is missing updated jar in classpath folder.
If you are using latest version of jdbc jar make sure it correctly points to classpath where the jar is located.
Hint: put some debug statement before invoking of class so that you can be sure correct jar is invoking everytime.

Unable to connect to SQL Server via JDBC. No suitable driver found for jdbc:sqlserver://

I simply want to use SQL Server database in my HTTP Servlet program but my program can't seem to connect to the database. It gives me the following error:
No suitable driver found for
jdbc:sqlserver://localhost:1433;databaseName=Bookyard;integratedSecurity=true;
This is my connection method.
package practice.bookyard.server.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
public static Connection getConnection() {
String url = "jdbc:sqlserver://(LocalDb)\\MSSQLLocalDB:1433;databaseName=Bookyard;integratedSecurity=true;";
Connection connection = null;
try {
connection = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
I had the sever name as localhost:1433 earlier but I changed it to the SQL Server instance name (LocalDb)\\MSSQLLocalDB:1433 but it still seems to pick up the old name.
Also, I am not sure how to provide the right connection string when connecting to SQL Server localdb.
I am using Eclipse for Java EE, Mars 2, and I downloaded Microsoft JDBC drivers for SQL 6.0 from this website.
I ran the installation, unzipped the contents of the resulting folder. Then, I added the sqljdbc42.jar file to the build path as I am targeting JDK 1.8.
UPDATE
Upon Scary Wombat's suggestion, I have also added the path to the sqljdbc42.jar file to my classpath.
However, I still get the same error.
I am pretty confident this is a reflection issue, in that the type loader isn't able to resolve the driver type from my connection string. Which means, the connection string syntax I am using is wrong.
I changed my connection string to read as follows:
String url = "jdbc:sqlserver://localhost:1433;
instance=(LocalDb)\\MSSQLLocalDB;
databaseName=Bookyard;integratedSecurity=true;";
However, I still not only get the same error but the exception message I receive still has my old connection string. So, clearly, there's also some caching going on, I just don't know where. Who is caching my connection string and how do I refresh / clear that cache?
Could you please tell me how to provide a SQL Server instance name if I am connecting to localdb and not on the main SQL Server instance?
As #ScaryWombat and #JozefChocholacek had hinted, it turned out to be a class path issue. Apparently, you have to copy and paste just the sqljdbc42.jar file, and this file only directly into the WEB-INF\lib folder and not within any sub-folder.
I did that it still gave me that error.
That was because the WEB-INF folder structure, when I viewed it in the Project Explorer, still had the old folder structure. So, I right-clicked on the WEB-INF folder in the Project Explorer and selected the Refresh command.
I also updated my environmental variable CLASSPATH to point it to the WEB-INF folder and that error went away.
you can do like below
String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);
note :however its not necceaary for new version of jdbc driver to load driver class manually using class.forname

No suitable driver found for jdbc java Tomcat 8

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

Connection to a derby database in a dynamic web project

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

How can I connect Access Database with Netbeans on Mac OSX?

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.

Categories