how to connect intelliJ to oracle - java

I have installed oracle and created table in it. now I want to connect intellij idea to oracle. I have added classes12.jar to libraries, but I can't connect my code to oracle. what should I do? my code is:
package example;
import java.sql.*;
public class first {
private Connection connection;
private Statement statement;
public first()throws Exception
{
Class.forName("oracle.jdbc.driver.oracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:ORCL","maryam","myjava123");
statement = connection.createStatement();
}
public void insert() throws Exception
{
statement.executeUpdate("INSERT INTO T1 (ID,NAME) VALUES (1,'ALI')");
}
public void close() throws Exception
{
statement.close();
connection.close();
}
public static void main(String[] args)throws Exception {
first mari=new first();
mari.insert();
}
}
and the Error is:
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.oracleDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at example.first.<init>(first.java:9)
at example.first.main(first.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Change
Class.forName("oracle.jdbc.driver.oracleDriver");
//--------------------------------^-- here is the issue
to
Class.forName("oracle.jdbc.OracleDriver");
or
Class.forName("oracle.jdbc.driver.OracleDriver");
Additionally, check if the driver jar file is really present in the classpath.

Use ojdbc6.jar or ojdbc7.jar in classpath. And also change oracleDriver to OracleDriver.

Related

JavaFX application with JDBC in NetBeans don't start

I work on a small educational project - easy java app with database and gui. I work in NetBeans IDE.
For graphical interface I use JavaFX, and as I've testet this part work perfectly fine, but I have some problem with JDBC part (I use JDBC driver for sqlite database, created with DB browser for sqlite). My main application class looks like this:
public class BazaLyoko extends Application {
#Override
public void start(Stage stage) throws Exception {
Database baza = new Database();
String tytul = baza.getEpisodeName(1);
stage.setTitle(tytul);
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
And when I change those lines:
Database baza = new Database();
String tytul = baza.getEpisodeName(1);
To this (to replace value retrieved from database to a placeholder - I did this to ensure if the error is rather connected with JavaFX or JDBC part)
String tytul = "";
Program start without problem, but when I try to use my database application don't start. There isn't any compilation error or anything like that (or maybe I just don't see them, as I'm quite new to this IDE, though I think such things are usually blatantly visible) just no application window appear.
Here's the code for my database class(they both share the same package):
public class Database {
public static Connection conn;
public Database()
{
connect();
}
public static void connect()
{
String connectionString = "jdbc:sqlite:BazaLyoko.db";
try
{
conn = DriverManager.getConnection(connectionString);
if (conn != null)
{
DatabaseMetaData meta = conn.getMetaData();
//System.out.println("Nazwa sterownika to " + meta.getDriverName());
//System.out.println("Stworzono baze danych.");
}
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private ResultSet doQuery(String query) throws SQLException
{
Statement stmt = null;
try
{
stmt = this.conn.createStatement();
ResultSet result = stmt.executeQuery(query);
return result;
}
catch (SQLException e )
{
throw new Error("Problem", e);
}
finally
{
if (stmt != null) { stmt.close(); }
}
}
public String getEpisodeName(int nr) throws SQLException
{
ResultSet result = doQuery("SELECT tytul FROM Odcinki WHERE numer="+String.valueOf(nr));
result.next();
return result.getString("tytul");
}
}
Database of that name is located inside my project directory (i also tried to copy it to package directory but nothing changed), the names of the table and column are correct to and exist a row with value 1 in column numer.
What I'm missing or doing wrong?
Edit:
I manage to get the following Stack Trace:
No suitable driver found for jdbc:sqlite:BazaLyoko.db
at com.sun.corba.se.impl.util.Utility.printStackTrace(Utility.java:933)
at bazalyoko.Database.connect(Database.java:46)
at bazalyoko.Database.<init>(Database.java:26)
at bazalyoko.BazaLyoko.start(BazaLyoko.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Thread.java:748)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at bazalyoko.Database.doQuery(Database.java:57)
at bazalyoko.Database.getEpisodeName(Database.java:75)
at bazalyoko.BazaLyoko.start(BazaLyoko.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Exception running application bazalyoko.BazaLyoko
Java Result: 1

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception in linux and text editor [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
My code looks like this-(working in normal text editor)
import java.sql.*;
import java.sql.DriverManager;
class JDBCTest {
private static final String url = "jdbc:mysql://localhost/learn";
private static final String user = "root";
private static final String password = "B!SHu12345";
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from bishu where id =101");
while(rs.next()){
String s=rs.getString("id");
System.out.println(s);
}
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
At run time it gives an exception as follows-
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at JDBCTest.main(first.java:14)
Mysql connector was downloaded and saved in the path- Java/jre1.8.0_91/lib/ext/
I have gone through many similar solutions available, but wasn't able to find a valid one.
Add the MYSQL JDBC Driver from the library of your IDE.or place this JDBC driver file inside the lib directory of your project. Your question looks similar to this question

Error connecting to SQ Server from Java: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

I am using this code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQLServer
{
public void dbConnect(String db_connect_string, String db_userid, String db_password)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select * from sysobjects where type='u'";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
connServer.dbConnect("jdbc:sqlserver://C1A\\SQL1A", "P", "G31les");
}
}
But I get this error:
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;C:\Users\wmoscoso\Desktop\SaturnLittleBrother\out\production\SaturnLittleBrother;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain ConnectMSSQLServer
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at ConnectMSSQLServer.dbConnect(ConnectMSSQLServer.java:16)
at ConnectMSSQLServer.main(ConnectMSSQLServer.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 0
I am new to Java, could somebody point me in the right direction? I don't know how to set a class path is that is needed? why do I even need to do that?
I got it to work. I need to download the jar files for a JDBC connection from the Microsoft website and then added them to my project.

Getting error while Connecting to oracle XE with NetBeans IDE

I am getting the following error while executing this code
import java.sql.*;
public class DatabaseConnectivityTest {
public static void main(String args[]) throws ClassNotFoundException
{
Connection conn=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Trying to connect to database");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hr","hr");
System.out.println("Connected");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
The error that i am getting is
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at DatabaseConnectivityTest.main(DatabaseConnectivityTest.java:10)
right click on your project folder --> Properties --> Java Build Path --> Libriaries --> Add External Jar --> Your sql connector jar file directory
i hope i was able to help you :)
this is due to you have not included oracle driver in the classpath
If you are using eclipse then try the following
right click on your project
click buildpath->configure build path
click libraries tab
click add external jars and give the path of oracledriver jar file
Try this code:
import java.sql.*;
public class DatabaseConnectivityTest {
public static void main(String args[]){
Connection conn=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e) {
System.err.println("The driver is not loaded properly");
}
try
{
System.out.println("Trying to connect to database");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hr","hr");
System.out.println("Connected");
}
catch(SQLException e)
{
System.err.println("Error while connecting!");
}
}
}
This code will give you clear idea where your program is failing. If you are getting a message "The driver is not loaded properly then add them to the build path. Download the correct jar file and add them to your project.

java.lang.ClassNotFoundException

I've installed MaxDB in my local machine and I'm trying to make a connection to it using Java.
And I'm getting this error when running :
Exception in thread "main"
java.lang.ClassNotFoundException:
com.sap.dbtech.jdbc.DriverSapDB
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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sap.maxdb.Hello.main(Hello.java:15)
This is the code I'm using :
package sap.maxdb;
import java.sql.*;
public class Hello
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
String username = "DBM";
String password = "azerty";
String dbname = "SAPDB";
Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");
String url = "jdbc:sapdb://" + dbname;
Connection connection = DriverManager.getConnection (url, username, password);
Statement stmt = connection.createStatement ();
ResultSet resultSet = stmt.executeQuery ("SELECT * FROM HOTEL.CUSTOMER");
resultSet.next ();
String hello = resultSet.getString (1);
System.out.println (hello);
resultSet.close ();
stmt.close();
connection.close ();
}
}
I did like they said in their website :
set CLASSPATH=%CLASSPATH%;C:\Program
Files\sdb\programs\runtime\jar\sapdbc.jar
But I get always the same error.
I know that I'm missing something but can't find it °!°
Waiting for your help.
Thanks.
What does "install" mean? Basically, the db server must be installed and running.
Step two is to add the driver (which you can find under C:\Program Files\sdb\programs\runtime\jar\sapdbc.jar, assuming this applies to your installation too) to the build path of your project. If the driver class cannot be found, it'll raise a ClassNotFoundException as you experienced.
You don't mention which IDE you are using, but try to set your project buildpath from the context menu.
Try adding the following code to make sure that your application picks up the CLASSPATH that you specified:
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
Copy the driver's jar file to the 'lib' folder of the server. Then restart the server.

Categories

Resources