The import oracle.jdbc cannot be resolved - java

I am currently working on a project where we have to connect to a database and through Java insert values into our database. Our professor gave us code in order to help us see how to carry that out. I am new to Java and have very little experience in it but I have been watching videos and researching online. My problem is the following: I am working in eclipse and have created a class called _DataGenerator_ all the code that is there is from my professor.
import java.sql.*;
import oracle.jdbc.driver.*;
public class TestDataGenerator {
public static void main(String args[]) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String database = "jdbc:oracle:thin:#131.130.122.4:1521:lab";
String user = "a+MatrNr";
String pass = "Oracle-Passwort";
// establish connection to database
Connection con = DriverManager.getConnection(database, user, pass);
Statement stmt = con.createStatement();
// insert a single dataset into the database
try {
String insertSql = "INSERT INTO person VALUES ('012345678902', 'Erich', 'Schiküta', 'Wien', 1010, 'Rathausstrasse 19', '12-FEB-2000', 'Wien')";
stmt.executeUpdate(insertSql);
} catch (Exception e) {
System.err.println("Fehler beim Einfuegen des Datensatzes: " + e.getMessage());
}
// check number of datasets in person table
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM person");
if (rs.next()) {
int count = rs.getInt(1);
System.out.println("Number of datasets: " + count);
}
// clean up connections
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
The only error I get when trying to run the code is from
import oracle.jdbc.driver.*;
When I put my cursor over the error symbol it says
the import oracle.jdbc cannot be resolved
When I try to run the code the only message I get back is
the statement in Red oracle.jdbc.driver.OracleDriver
I don't know what the problem is. I don't know if it helps to know that I have created my database in oracle SQL developer.

In eclipse, right click your project->Build Path->Config Build Path->find the Libraries tab and press the Add External Jars, locate your oracle jdbc driver in your hard driver and select it. Make sure it appears in the jars list, and then press apply and close.
you can find the oracle jdbc driver in the offical website:
https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html.
After that this issue should disappear.

Well,the jdbc lib of official oracle is not in maven repo,you should download it from Oracle Website and maven install your path.And if you use maven build your project,you can do this:
mvn install:install-file -Dfile=E:/app/Administrator/product/11.2.0/dbhome_1/jdbc/lib/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
Then add to dependency like :
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>

Related

jdbc connection established but didnt print the result in eclipse console

I am new to this forum....
Iam tring to connect java project created in eclipse with oracle database xe,
I followed the procedures as descrided in the following link
https://www.youtube.com/watch?v=fMp63HsIRbc
but it didnt print the result in the eclipse console but it also didnt throw the exception as well
what should be the reason
this is my code
package dbmsoracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Jdb {
public static void main(String[] args) {
System.out.println("jen");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","system","murali123");
Statement st = con.createStatement();
String sql="select * from muhil";
ResultSet rs =st.executeQuery(sql);
while (rs.next())
{
System.out.println("executing ...");
System.out.println(rs.getInt(1)+" "+rs.getInt(2));
System.out.println("executed");
con.close();
}
}
catch (Exception e)
{
System.out.println("connection failed");
System.out.println(e);
}
}
i have downloaded my jdbc driver
Oracle Database 11g Release 2 (11.2.0.4) JDBC Drivers
ojdbc6.jar
Certified with JDK 8, JDK 7 and JDK 6: ......
and iam using jdk 8
Firstly check that your connection is establishing with oracle database or not. Here is the connection code try this
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class OracleJDBC {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe","system","murali123");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
I have the same problem. The problem is your code doesn't catch(SQLException e). Because just catch(Exception e) will not make the exception of sql shown in the console. After doing this, you will probably find the Exception is "No suitable driver found for jdbc".
Then, either you can download the mysql-connector-java and add it to your lib, or if your project is Maven, you can just add dependency in your pom file.
After you used Sql+ for entering database, commit the table. Enter 'commit'(without quotes) on the terminal or either close your Sql+ terminal. In your case, you must have closed the terminal after you changed your id from 24, 253 to 1, 2. That is why it ran properly. Got nothing to do with id starting from 1
first of all go and check if your table has any data or not. It seems you just created this table 'muhil' before writing the above code. i'm pretty sure You must have entered some data as well in your new table. Just make sure you have Committed your transaction in the MYSQL Workbench. Just commit it and try to run your code in eclipse again.

Creating a java program which getting SQLite data and Insert in Oracle DB

I've just started my adventure with programming in SQL using JAVA.
I have Linux and Oracle Database on VirtualBox Machine.
My project is about situation, when a client give me a SQLite3 Database and I have to convert it to Oracle Database.
I've read some code but it gives error
Code:
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "Move" AS
import java.sql.*;
import java.io.*;
public class getting {
public static void doIt() throws Exception{
Connection conn;
ResultSet rs;
Statement stat;
try{
Class.forName("org.sqlite.JDBC");
conn =
DriverManager.getConnection("jdbc:sqlite://127.0.0.1/media/sf_SHARE/baza.db");
} catch (SQLException e){
throw new RuntimeException(e);
}
try{
stat = conn.createStatement();
try{
rs = stat.executeQuery("SELECT * from entities");
while(rs.next()){
String w1= rs.getString("ID");
String w2 = rs.getString("TEXT");
System.out.println(w1+w2);
}
}finally{}
}finally{}
{try {rs.close();
}catch (Exception ignore){}
try {conn.close();
}catch (Exception ignore){}
try {stat.close();
}catch (Exception ignore){}
}
}
}
/
create or replace function Move return varchar2 as
language java name 'getting.doIt() return java.language.String';
/
select Move from dual;
I've got a error:
java.lang.ClassNotFoundException: org/sqlite/JDBC
Is connection in DriverManager.getConnection() is good?
Any ideas what I did wrong?
Cheers
najdzion
You have to download JDBC driver from here: https://bitbucket.org/xerial/sqlite-jdbc/downloads and import downloaded JAR to your project.
Also, as good practice, don't store connections as hard-coded strings.
If you are using maven, check your dependencies:
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.2</version>
</dependency>
Add your jar co Classpath:
> javac Example.java
> java -classpath ".;sqlite-jdbc-(VERSION).jar" Example # in Windows
or
> java -classpath ".:sqlite-jdbc-(VERSION).jar" Example # in Mac or Linux
or you can do it via your IDE.

Problems connecting to sqlite database in java

I am following a tutorial on connecting my SQLite database to a Java application.
When I run the program I get the following error in the console of NetBeans:
run:
Error connecting to the databasejava.sql.SQLException: No suitable
driver found for jdbc:C:\Users\lawman\Documents\Java Working
Directory\LoginSql\src\project123.sqlite
BUILD SUCCESSFUL (total time: 0 seconds)
Here is my directory:
I have code to connect to the database in class tobecalledbymain.
I have main in mainclass which creates an instance of tobecalledbymain.
In my library file, I have sqlite-jdbcs.jar imported.
Here is the code for tobecalledinmain:
import java.sql.*;
public class tobecalledinmain {
public tobecalledinmain(){
Connection con = null;
Statement st=null;
ResultSet rs=null;
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:C:\\Users\\lawman\\Documents\\"
+ "Java Working Directory\\LoginSql\\"
+ "src\\project123.sqlite");
st=con.createStatement();
//select all records from the table employee
//table has three firlds: employeeid,name and surname
rs=st.executeQuery("SELECT * FROM Employee;");
while (rs.next())
{
int id = rs.getInt("Employeeid");
String name = rs.getString("Name");
System.out.println("id = " + id);
System.out.println("name= " + name);
System.out.println();
}
rs.close();
st.close();
con.close();
}catch(Exception e)
{
System.out.println("Error connecting to the database" + e);
}
}
}
Here is the mainclass code:
public class mainClass {
public static void main(String[] args){
new tobecalledinmain();
}
}
;;
I am not sure why we need to semi-colons!
Anyway, when the tutorial concludes he gets a result from the console. I get the said error message.
What are the drivers in the error message referring to and how do I get them?
Your jdbc connection string does not specify sqlite. Try this, and use forward slashes.
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/PATH/TO/database.db");
This was the WRONG answer, pls disregard.
You need to add the .jar for the SQLite database driver to your classpath when you run your code. See https://bitbucket.org/xerial/sqlite-jdbc
You can see how to do that in Netbeans here: How to add a JAR in NetBeans

Java "Connection" Class does not connect to the DB

I'm trying to connect my DB with a Java Application i'm creating. What I got so far:
public class DBConnect {
public void DBConnect() {
try {
DBConnect DBConnect = null;
String url = "jdbc:mysql://localhost:3306/ähs_system";
String uName = "**";
String uPass = "**";
// Connection conn = DriverManager.getConnection(url, uName, uPass);
System.out.println("DB Connected");
}
catch (Exception Err) {
System.out.println("Error while connecting: " + Err.getMessage());
System.exit(0);
}
}
}
It's a runnable code although i'm still able to run the code without any error messages if I change my uName and/or Upass. So based on that information i'm gonna say that it's not actually connecting to the database at all...
Anyone with a few tips or tricks I can use?
I've loaded the DB in services and I am able to reach it and add data and run other SQL commands within netbeans but that's basically it. I've also loaded the mysql-connector-java-5.1.35 driver.
Run Code:
public static void main(String args[]) {
try {
DBConnect DBConnect = new DBConnect ();
DBConnect.DBConnect();
}
catch (Exception e){
System.out.println("Cannot connect to DB. Error: " + e.getMessage());
}
Let me know if you need any furthur information!
Updating property file: C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\build\built-jar.properties
Compiling 1 source file to C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\build\classes
C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\src\kiltenros\DBConnect.java:23: error: incompatible types: java.sql.Connection cannot be converted to kiltenros.Connection
Connection conn = DriverManager.getConnection(url, uName, uPass);
1 error
C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
the error log is clear
java.sql.Connection cannot be converted to kiltenros.Connection
it seems that you imported the wrong class in the beginning of your class
DriverManager.getConnection(url, uName, uPass) return you instance of java.sql.Connection.
btw, change your local variable DBConnect to dbConnect so it won't has the same name as your class DBConnect and it will follow the java convention (lowercase on 1st letter of a variable)
I've very little experience with MySQL, but I avoid using "ä" in a database name. Perhaps, the underscore isn't allowed.
As per your stacktrace it seems you have not imported the correct Connection class. Delete the Connection file in your project and import java.sql.Connection.
The problem seemed to be a corrupted rs2xml.jar file. Once I reloaded it and it worked.

Java Database Connectivity doesn't work

I have problem to connect to my Database from my Java Code. I am using MS Access Database. I have created DSR Name and selected appropriate Database for my DSR Name.
DSR Name : connectionExample
Operating System : Windows 8 Pro (64 Bit).
Please answer me with full tutorial because I am a newbie.
My Java Code for Database Connection Example is :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DatabaseConnection {
public static void main(String[] args) {
String uName="ABC",uPass="ABC",uEmail="ABC",uDate="ABC",uContactNo="ABC";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
Connection con = DriverManager.getConnection("Jdbc:odbc:connectionExample");
Statement stmnt = con.createStatement();
String sql = "insert into UserDetail values ('"+uName + "','"+uPass+"','"+uEmail+"','"+uDate+"','"+uContactNo+"')";
int cnt = stmnt.executeUpdate(sql);
System.out.println("Database Updated.");
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
The Output of my code is :
run:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
BUILD SUCCESSFUL (total time: 0 seconds)
You must add connecter jar file to netbeans project library.
Download jdbc connector jar file.
Copy it to project folder
select project in netbeans choose properties from right click menu.
Select library->add jar file/folder
select jdbc file.
Now your code will work.

Categories