java connectivity with mysql error - java

I just started with the connectivity and tried this example. I have installed the necessary softwares. Also copied the jar file into the /ext folder.Yet the code below has the following error
import java.sql.*;
public class Jdbc00 {
public static void main(String args[]){
try {
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url =
"jdbc:mysql://localhost:3306/mysql"
DriverManager.getConnection(url,"root", "root");
//Display URL and connection information
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
//Get a Statement object
stmt = con.createStatement();
//Create the new database
stmt.executeUpdate(
"CREATE DATABASE JunkDB");
stmt.executeUpdate(
"GRANT SELECT,INSERT,UPDATE,DELETE," +
"CREATE,DROP " +
"ON JunkDB.* TO 'auser'#'localhost' " +
"IDENTIFIED BY 'drowssap';");
con.close();
}catch( Exception e ) {
e.printStackTrace();
}//end catch
}//end main
}//end class Jdbc00
But it gave the following error
D:\Java12\Explore>java Jdbc00
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Jdbc00.main(Jdbc00.java:11)
Could anyone please guide me in correcting this?

The jar file that contains the MySQL driver class (com.mysql.jdbc.Driver) isn't being found on the classpath when you run your application. This is what the ClassNotFoundException is complaining about.
You'll need to add it either to the CLASSPATH environment variable, or using the classpath option when running Java. For example:
java -cp mysql-connector-java-5.0.8-bin.jar Jdbc00
Use the name and location of whatever MySQL connector jar file you're using. (If you haven't already installed MySQL on localhost, so your application has something to connect to, you might have to do that too.)

As #Ash says, the problem is that the Connector/J drivers are not on your classpath. You can download the latest version (5.0.12) from this page.

Related

Java issue within connection to Microsoft SQL Server

How can I connect to Microsoft SQL Server from my java code?
Code:
public class insert {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://LENOVO-PC\\SQLEXPRESS;DatabaseName=dbtest";
Connection connection = DriverManager.getConnection(url , "sa" , "Aa123456");
Statement st = connection.createStatement();
st.executeUpdate("INSERT INTO [dbo].[table] VALUES ('come')");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
Error:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at insert.main(insert.java:12)
Microsoft SQL Server name is: LENOVO-PC\SQLEXPRESS
and sqljdbc.jar is already added to the referenced libraries.
The class name com.microsoft.jdbc.sqlserver.SQLServerDriver you're trying to load is from a very old version of the Microsoft SQL Server 2000 version of the driver. Around 2005, Microsoft changed this to com.microsoft.sqlserver.jdbc.SQLServerDriver (notice the switch in order between jdbc and sqlserver). At that time, they also changed the driver URL prefix from jdbc:microsoft:sqlserver: to jdbc:sqlserver:.
In other words, you need to:
Use Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") (optional, driver will be loaded automatically)
Change your URL to jdbc:sqlserver://LENOVO-PC\\SQLEXPRESS;databaseName=dbtest (note the prefix change and DatabaseName -> databaseName. See also Building the Connection URL.
With recent JDBC drivers, it is not necessary to explicitly load the driver class in simple Java applications, so instead of step 1, you could also remove the Class.forName line.
Also make sure you are using a recent version of the driver, see the mssql-jdbc project on GitHub (latest stable version at time of writing is 7.0.0).
Correct the driver class name:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Java - Executing jar fails because of Prohibited package name: java.sql

I am making a multiple choice quiz that makes use of the sqlite-jdbc-3.21.0.jar driver. You can find it here:
https://bitbucket.org/xerial/sqlite-jdbc/downloads/
I've made a class called Database to handle everything relating to SQLite:
package quizpack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Database {
Console cnsl = new Console();
ResultSet rs = null;
Connection connection = null;
Statement stmt = null;
public Connection connect() {
try {
connection = DriverManager.getConnection("jdbc:sqlite:./data/data.db");
Class.forName("org.sqlite.JDBC");
} catch (Exception e) {
cnsl.println("connect() error: " + e.getMessage());
}
return connection;
}
}
When i run my quiz in Eclipse it doesn't give any errors. When i compile it it doesn't give any errors (packaged required libraries). As soon as i launch the jar file in commandprompt i get the message: Prohibited package name: java.sql.
That would be: java.lang.SecurityException: Prohibited package name: java.sql
This is the stacktrace of the connect() error:
connect() error: Prohibited package name: java.sql
java.lang.SecurityException: Prohibited package name: java.sql
at java.base/java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at java.base/java.security.SecureClassLoader.defineClass(Unknown Source)
at java.base/java.net.URLClassLoader.defineClass(Unknown Source)
at java.base/java.net.URLClassLoader.access$100(Unknown Source)
at java.base/java.net.URLClassLoader$1.run(Unknown Source)
at java.base/java.net.URLClassLoader$1.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at quiz.Database.connect(Database.java:21)
at quiz.Database.buildQuery(Database.java:51)
at quiz.Quiz.<init>(Quiz.java:22)
at quiz.Main.main(Main.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Nativ
e Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknow
n Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Un
known Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
What i tried:
Changing the package name.
Changing the sequence of my buildpath.
Tried several placings of where i put the driver jar in my source folder.
Signing my exported jar.
Added the jar to my classpathvariables:
database connection not working in jar, but does work in eclipse
Now i'm a beginning Java programmer, i might have done something wrong from the above list. But i'm hoping someone can shed light on what is actually causing this error to happen.
This seems like a repeat of Prohibited package name: java, but you said you changed your package name.
If you didn't change the package name from java to something else, try changing that. Else, delete your jar file and rebuild.

Cannot solve Classnotfound Exception

I hope the answer will be stupidly simple, but I cannot get the following test program running due to a classnotfound exception:
import java.net.URL;
import net.sourceforge.spnego.SpnegoHttpURLConnection;
public class HelloKeytab {
public static void main(final String[] args) throws Exception {
System.setProperty("java.security.krb5.conf", "krb5.conf");
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("java.security.auth.login.config", "login.conf");
SpnegoHttpURLConnection spnego = null;
try {
spnego = new SpnegoHttpURLConnection("spnego-client");
spnego.connect(new URL("http://as1.test.local/hello_spnego.jsp"));
System.out.println("HTTP Status Code: "
+ spnego.getResponseCode());
System.out.println("HTTP Status Message: "
+ spnego.getResponseMessage());
} finally {
if (null != spnego) {
spnego.disconnect();
}
}
}
}
I installed JDK7 and set the JAVA_HOME environment variable as an Administrator.
I am working on a Windows XP machine as a regular domain user while compiling and running.
I have the spnego-r7.jar in the same directory as the HelloKeytab.java and I compiled with:
javac -cp .;spnego-r7.jar HelloKeytab.java
which succesfully creates the class.
When I run the program with:
java -cp .;spengo-r7.jar HelloKeytab
I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: net/sourceforge/spneg
o/SpnegoHttpURLConnection
at HelloKeytab.main(HelloKeytab.java:15)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.spnego.SpnegoHttpUR
LConnection
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
The spnego-r7.jar can be found here: http://sourceforge.net/projects/spnego/files/
What I am doing wrong that it doesn't find my class?
NoClassDefFoundError are always because of incorrect or incomplete builds, or errors in class path.
I always go through the following checklist when getting this error:
Clean project
Check files and directories
Check build path
Try again. Never fails. :)

Can't connect to a mysql DB with DriverManager

Ok I am trying to connect to a mysql db with the following code. I tried looking at documentation but I am just having no luck what so ever.
Here is the code:
public class TESTSQL {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://SLibraryDev.db.996****.hostedresource.com";
static final String USER = "SLD";
static final String PASS = "F11!";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
...
}
This is the error I am getting :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
When I comment out "Class.forName(JDBC_DRIVER)" and run it I get this error:
java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
What I thought I needed to do was to download and run this : http://dev.mysql.com/downloads/connector/j/ which I did but it seemed like something was wrong with this installer...It seemed like it never finished, like it cut out in the middle of the process. Anyone have any ideas on what I need to do.
Download mysql-connector from following url
http://nearbuy.googlecode.com/files/mysql-connector-java-5.1.13-bin.jar
and add it to your class path.
If you are using netbeans, to add this file to your project do followoing
Right click on your project>click libraries>click add JAR/Folder> then select downloaded jar file
You need to download the mysql jdbc driver jar and include it in your classpath
java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
clearly suggests that there is no driver in your class path
simply add the mysql driver jar file in to class path

MySQL jdbc driver and Eclipse problems

I am having problems loading the MySQL JDBC driver. I have tried everything mentioned here, but am still running into problems.
The error I get is this:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Main.java:12)
My code is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
public static void main(String args[]) throws Exception {
//accessing the driver
Class.forName("com.mysql.jdbc.driver");
// creating variable to pass the connection information into
Connection dbcon = DriverManager.getConnection(
"jdbc:mysql//localhost:3306/test", "root", "root");
PreparedStatement statement = dbcon
.prepareStatement("select * from names");
ResultSet result = statement.executeQuery();
while (result.next()) {
System.out.println(result.getString(2));
}
}
}
As you can see above I have placed the driver in the lib folder and I have also put it into the Apache tomcat lib folder, because that was suggested as well.
Thank you for any help you can give me.
Andrew
The class name inside the MySql connectorJ JAR file is Driver. so you need to change
Class.forName("com.mysql.jdbc.driver");
to
Class.forName("com.mysql.jdbc.Driver");
It tells you the problem on the first line of your stacktrace:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
The driver name is com.mysql.jdbc.Driver (capital D).
problem in loading the mysql connection library.please check the proper mysql library is load successfully.because when library load successfully then the library package display all the external loaded library.

Categories

Resources