with root cause java.lang.ClassNotFoundException: com.mysql.jdbc.Driver - java

I've this little code for connect my jsp to my mysql db.
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url="jdbc:mysql://localhost:3306/test1";
Connection con = DriverManager.getConnection(url, "test1", "test1");
Statement cmd = con.createStatement();
String query = "SELECT * FROM champions";
ResultSet res = cmd.executeQuery(query);
while (res.next()) {
System.out.print(res.getString("name") + ", ");
System.out.println(res.getString("title"));
}
res.close(); // chiudere le risorse DB è obbligatorio
cmd.close();
con.close();
I've added the connector to my build path:
But i'm still get this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Someone can help me?

Build path is for compilation purpose (assuming you don't have code to include mysql connector jar in package).
If this is web application, add it to lib folder.
If this is standalone application, make sure mysql connector jar available for runtime.

Related

JDBC Driver Does Not Exist

I’m trying to connect a Java program to a remote Oracle DB. After doing some research online, I decided that the easiest way to do this was with the Oracle JDBC driver. I downloaded and ran the jar file and got the message “***** JCE UNLIMITED STRENGTH IS INSTALLED *****.” The problem is that when I try to add the driver to my classpath (javac -classpath ojdbc8.jar Connect.java), I keep getting an error message saying “package oracle.jdbc.driver does not exist.” I’ve been researching how to fix this online, but I’m only getting confused. Any ideas on what I did wrong?
import java.sql.*;
public class Class1 {
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You must put a database name after the # sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as `<host>`:`<port>`:`<sid>`. The example uses the short cut syntax.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:hr/hr#myhostname:1521:orcl",
"myUsername", "myPassword");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
conn.close(); // ** IMPORTANT : Close connections when done **
}
}
The error is:
java: package oracle.jdbc.driver does not exist
Can you try to run the sample DataSourceSample.java? Make sure you have the JDBC driver in the classpath. You can also refer to this quickstart for detailed instructions.

Is it a matter of connection between my workspace and the data base or what?

I've created a data base in Mysql; and a forum in a jsp page to verify the data received but But I've encountered a problem and I do not know the reason
error :message An exception occurred processing JSP page /massar.jsp at line 24
Code :
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/massar";
String user = "root";
String password = "!!!!";
String sql = "select massar_profil from massar.massar_user";
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url, user, password);
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
%>
'
If the error occured on this line:
Class.forName(driverName).newInstance();
then the most likely explanation is that the Class.forName(driverName) has failed to load the JDBC driver. And the most likely explanation for that is that the JAR file containing the JDBC driver has not been included in the runtime classpath for your webapp. Check that the JAR is in your WAR file's "/WEB-INF/lib" directory.
If this doesn't solve your problem, please get your JSP to produce a stacktrace, and show it to us.

I am unable to run my program

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=student.mdb;DriverID=22;READONLY=true
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at withoutdsn.Main.main(Main.java:26)
Java Result: 1
In order to successfully execute a connection with a database through java,for example if you are using mysql follow the steps below:
Go to mysql website and download the appropriate driver for Java.
Then go to Project -> Properties -> Java Build Path -> Libraries (in Eclipse) and click on "add external Jars".
Add the .jar you downloaded.
Before doing anything else you should be sure to set your connection right.
For example:
//the default port of mysql is 3306
String url = "jdbc:mysql://127.0.0.1:3306/mydb";
String login = "root";
String passwd = "toor";
Connection cn = null;
Statement st = null;
ResultSet rs = null;
System.out.println("Connecting to database..");
try {
cn = DriverManager.getConnection(url, login, passwd);
System.out.println("Database Connected");
st = cn.createStatement();
String sql = "SELECT * FROM impacts";
rs = st.executeQuery(sql);
while (rs.next()){
//do something
}
}catch(Exception e){
System.out.println("Exception");
}finally{
if (cn != null ){
cn.close();
}
}
Are you using Java 8 and a JDBC-ODBC Bridge Driver? Bridge drivers are deprecated and not available in Java 8. See here.
If you want to run simple JDBC programs, instead of MS Access you could try using Java DB. Probably, Java DB works best with Netbeans IDE. There is nice tutorial here.

Sqlite: Execute Spatialite Functions in Intellij

Does anyone know whether it is possible to execute spatialite functions on sqlite-databases in intellij?
Reference of spatialite functions: http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.0.html
In particular, I would be interested in using functions on type Point.
Thanks!
Edit: Functions do work within the official spatialite-gui, however I don't think there is a way to use the spatialite-gui programmatically, is there?
Here is what I tried so far: In intellij I connected the Java JDBC library and tried using function ST_X(point) and ST_Y(point) with no success:
Connection c = null;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + path + databaseName);
c.setAutoCommit(false);
System.out.println("Opened database \"" + databaseName + "\" successfully");
String sql = "SELECT id, ST_Y(point), ST_X(point) from tablename;";
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while ( rs.next() ) {
String msg = "Id: ";
msg += rs.getInt(1);
msg += " , Latitude: ";
msg += rs.getDouble(2);
msg += " , Longitude: ";
msg += rs.getDouble(3);
System.out.println(msg);
}
rs.close();
stmt.close();
c.close();
This throws the following exception:
Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such function: ST_Y)
at org.sqlite.core.DB.newSQLException(DB.java:890)
at org.sqlite.core.DB.newSQLException(DB.java:901)
at org.sqlite.core.DB.throwex(DB.java:868)
at org.sqlite.core.NativeDB.prepare(Native Method)
at org.sqlite.core.DB.prepare(DB.java:211)
at org.sqlite.jdbc3.JDBC3Statement.executeQuery(JDBC3Statement.java:81)
at com.company.Test.main(Test.java:77)
Edit 2:
I'm lost. It seems that I need to load extensions to make it work. Is this not included in the JDBC connector? (I pulled the JDBC connector through Maven.)
Where do I find the correct extensions?
Are those the ones? https://www.gaia-gis.it/fossil/libspatialite/index
Or those? http://www.gaia-gis.it/gaia-sins/index.html
How do I use them? Has anybody done this before?
Before performing spatial queries you need to load the spatialite module doing
SELECT load_extension('mod_spatialite');
You can find the documentation here: Dynamically loading SpatiaLite as an extension module.
Note that the module to load must be on the system path (documentation) and the path separator must be /. Also take into account that the spatialite module and Java must be compatibles (both 32 or 64 bits).

How do I find Oracle 12c connection URL?

I am using eclipse and java to connect to an oracle 12c database. But every time I run through the process, I get a connection fail error that reads:
java.sql.SQLException: Invalid Oracle URL specified
Here is what my code looks like:
import java.sql.*;
public class JdbcConnection {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin#localhost:1521:xe", "username", "password");
Statement statement = con.createStatement();
String sql = "select * from employees";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
System.out.println(rs.getInt(3) + " " + rs.getString(2));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
I am using a windows 7 sp1 Ultimate (64 bit)
CPU: i5 2.85 ghz
RAM: 8GB
Oracle is installed in a separate hard drive (in the same computer).
EDIT:I have tried the first suggestion from this post: How do you find out the Oracle database's URL?
and I get an error that says:
ORA-00928: SELECT KEYWORD MISSING...
This error occurs when I run the oracle "SQL DEVELOPER" application on the startup menu and type the suggestions from post above.
Note: I want to connect to the database from eclipse and query it programatically.
Is there a way to find out what the correct connection URL is?
Please help.

Categories