Exception with com.mysql.jdbc.Driver - java

i'm trying run code
public static Connection getConnection() throws SQLException{
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull [root on Default schema]");
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null,e.toString());
}
return cn;
}
but i get the exception:
with the dialog:
I have added the divier :mysql-connector-java-5.1.36-bin.jar in this project.
What am I doing wrong?

Add mysql connector jar to your project classpath.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

Related

Java, Maven, connect SQL , no suitable driver

I am building a project using Netbeans IED, with java. The project is using maven and I am attempting to connect it to sql database which I am having issue's. The code works in java but not with maven.
Here the error:
No suitable driver found for jdbc:derby://localhost:1527/Database
Java Code:
public class DatabaseTest {
public static Connection ConnectionObj = null;
public static Statement SqlStatement = null;
public static ResultSet Sqlresult = null;
public static ResultSetMetaData MetaData = null;
public static String query = "Select * from Wallet";
public static String url = "jdbc:derby://localhost:1527/Database";
public static String user = "ABM";
public static String pass = "password2";
public static void main(String[] args) {
try {
//Allows you to connect the database
ConnectionObj = DriverManager.getConnection(url, user, pass);
SqlStatement = ConnectionObj.createStatement();
Sqlresult = SqlStatement.executeQuery(query);
MetaData = Sqlresult.getMetaData();
System.out.println("Connection worked");
} catch (SQLException e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Prom depency's:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.1.0</version>
</dependency>
https://gyazo.com/8937aada3bd4a8f5b108b5dc9b386dd7
This part of your POM file is incorrect:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
Your program is attempting to use JDBC to connect to a Derby database, so you should be using a Derby JDBC driver, not a MySQL JDBC driver.
Replace the above with the following:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.1.0</version>
</dependency>
(Use the same version as your main Derby version ...)
The code works in Java but not with Maven.
Curious. Perhaps you are setting the runtime classpath correctly in the Java case.

ClassNotFoundException when start tyrus standalone server

I'm getting the following error on server.start() call:
ClassNotFoundException: org.glassfish.tyrus.container.grizzly.server.GrizzlyServerContainer
I also could not found a maven dependency for GrizzlyServerContainer.
Any ideas? Here is my code:
private WebSocketModulManager() {
server = new Server("127.0.0.1", 30000, "/lightconsole", null, LightconsoleEndpoint.class);
try {
server.start();
} catch (DeploymentException e) {
e.printStackTrace();
} finally {
server.stop();
}
}
Maven Dependecies
Try this dependency:
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-container-grizzly-server</artifactId>
<version>1.13</version>
</dependency>

Servlet with JDBC [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 5 years ago.
I have a problem with my progect. Files of progect:
House.class
public class House implements Serializable {
//properties -------------------------------------------------------------
private String price;
private String square;
private String RoomNumbers;
//------------------------------------------------------------------------
//getters - settersm Object overriding.... -----------------------------
HouseDAO.class
public class HouseDAO {
Connection connection;
final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb2";
final String DB_USER = "root";
final String DB_PASSWORD = "root";
public HouseDAO(Connection connection) {
this.connection = connection;
}
public List<House> getList() {
List<House> houses = new ArrayList<>();
try {
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
System.out.println("Connection available");
PreparedStatement ps = connection.prepareStatement("SELECT Square, RoomNumbers, Price FROM houses WHERE District = 'Dnepr'");
ResultSet rs = ps.executeQuery();
while (rs.next()){
House house = new House();
house.setSquare(rs.getString("Square"));
house.setRoomNumbers(rs.getString("RoomNumbers"));
house.setPrice(rs.getString("Price"));
houses.add(house);
}
}catch (Exception ex){
System.out.println("SQL exceprion");
ex.printStackTrace();
}
return houses;
}
}
and Servlet:
HousesBaseServlet
#WebServlet("/post")
public class HosesBaseServlet extends HttpServlet {
Connection conn;
private HouseDAO houseDAO;
#Override
public void init(){
houseDAO = new HouseDAO(conn);
}
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
//choice from html form
// String choice = request.getParameter("district");
try {
List<House> houses = houseDAO.getList();
request.setAttribute("houses", houses);
request.getRequestDispatcher("/houses.jsp").forward(request,response);
}catch (Exception ex ) {
System.out.println("Fail to connect with base");
ex.printStackTrace();
}
}
}
I was read some solutiotuns, but it doesn't help. The problem in two exceptions:
SQL exceprion java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/mydb2
I try to add:
Class.forName("com.mysql.jdbc.Driver");
to my code, and add mysql connector jar to my project, but it throws exception:
SQL exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Second exception:
JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core
cannot be resolved in either web.xml or the jar files deployed with
this application Fail to connect with base
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
,
JSP taglib:
%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %
and project structure:
[project structure][1]
Project structure - artifacts
In project structure -> libraries i have all jars.
Since you are using IntelliJ I believe you might need to add the libraries to the artifact because from my experience Intellij adds the maven dependencies to the Classpath but not to the artifact.
Make sure you go to File -> Project Structure -> Artifacts and then add all the libraries from the available side to the artifact.
But you need to register the driver before getting the connection otherwise it doesn't work either way :
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
Hope this helps.

Jersey: The mysql java.lang.ClassNotFoundException: com.mysql.jdbc.Driver although inclueded

I am trying to send GET request from the postman chrome plugin and I am getting this error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver I have already included the mysql-connector-java-5.1.35-bin in my project
jersey endpoint:
#Path("/test")
public class Driver{
#GET
#Produces(MediaType.TEXT_PLAIN)
public void mysqltest(){
Database db = new Database();
db.connection();
}
}
Database class:
public class Database {
public void connection() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("jar works :) ");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have already tested it with this class in the same project and I am getting the output driver works
Driver class:
public class Driver {
public static void main(String[] args){
connection();
}
public static void connection() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("driver works :) ");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Why when am I calling it from the jersey method I am getting the error in the title?
You need to add the following dependency in your pom.xml file. (I assume you are working with Maven project)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
P.S. I was getting the same exception. This worked for me.
Adding to what #LuiggiMendoza wrote, you need to understand the packaging of your artifacts and trace the classloader delegation to understand if the classloader that invokes your Driver class also has access to the MySQL driver JAR. For instance, adding the MySQL JAR to WEB-INF/lib may not help if Jersey is a dependency of an EJB JAR that itself is in the WEB-INF/lib of the webapp.

Getting error retrieving data from column family using cassandra-jdbc dreiver

when i am retrieving data from column family using Cssandra-JDBC driver. i got error
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cassandra.thrift.Cassandra$Client.execute_cql3_query(Ljava/nio/ByteBuffer;Lorg/apache/cassandra/thrift/Compression;Lorg/apache/cassandra/thrift/ConsistencyLevel;)Lorg/apache/cassandra/thrift/CqlResult;
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:447)
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:472)
at org.apache.cassandra.cql.jdbc.CassandraStatement.doExecute(CassandraStatement.java:161)
at org.apache.cassandra.cql.jdbc.CassandraStatement.executeQuery(CassandraStatement.java:226)
at CassandraJDBCTest.main(CassandraJDBCTest.java:19)
Code is
public static void main (String args[]) throws SQLException{
try {
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/TestExample");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Name,Age FROM Users WHERE keyname='001';");
rs.next();
System.out.println(rs.getString("Name"));
System.out.println(rs.getInt(2));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You have missing dependencies:
Exception in thread "main" java.lang.NoSuchMethodError
I have a feeling that the classpath is just not configured correctly (why the L's?):
Ljava/nio/ByteBuffer;
Lorg/apache/cassandra/thrift/Compression;
Lorg/apache/cassandra/thrift/ConsistencyLevel;
...
If you want to save yourself of dependency hell try using maven, the Datastax java driver has a maven central repo and you just need to include the dependency:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>1.0.2</version>
</dependency>
EDIT
Sorry, didnt realise you are using JDBC. The cassandra jdbc driver has also got a maven repository:
<dependency>
<groupId>org.apache-extras.cassandra-jdbc</groupId>
<artifactId>cassandra-jdbc</artifactId>
<version>1.2.5</version>
</dependency>

Categories