Can't connect Android Studio with Oracle Database 19c - java

I've recently tried to connect Android Studio with an OracleDB through ojdbc driver without success.
I'm using ojdbc8.jar, Oracle Database 19c and jdk1.8.0_241 on Windows10.
I tried to do the exact same thing with the same code and components in Intellij and everything went well.
When I try to connect to the database I received the following error:
see_error_print "java.lang.NoClassDefFoundError: Failed resolution of: Ljava/sql/DriverAction;" in the line Class.forName.
I have installed Android 28 and my gradle version is 3.6.2.. I tried to clean project, and invalidate caches/restart, but nothing seems to work.
In fact, in Android28 (Sdk) at java.sql, indeed the missing file isn't there, but exists in jdk.
I appreciate any help I can get, thank you!
P.S.: english is not my mother language, sorry if there are any mistakes.
Here is the code I used:
package com.lamtias.isplit.ActivityLogin;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import com.lamtias.isplit.Logic.ISplit;
import com.lamtias.isplit.R;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class SignUpActivity extends AppCompatActivity {
private ISplit iSplit;
private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DEFAULT_URL = "jdbc:oracle:thin:#localhost:1521:ISplitDB";
private static final String DEFAULT_USERNAME = "c##marcia";
private static final String DEFAULT_PASSWORD = "marcia";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
JDBCexample(DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
}
public static void JDBCexample(String dbid, String userid, String passwd)
{
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:ISplitDB", userid, passwd);
if(conn!=null)
System.out.println ("oiiiiiiiiiiiiiiiiiiiiiiiiiiii");
else
System.out.println ("oeeeeeeeeeeeeeeeeeeeeeeeeeeee");
Statement stmt = conn.createStatement();
//stmt.executeUpdate( "insert into users values ('1', 'Perryridge')");
stmt.close();
conn.close();
}
catch (SQLException sqle) {
System.out.println("SQLException : " + sqle);
} catch (Exception e){
System.out.println("Exception : " + e);
}
}
}

I have fixed the same problem using ojdbc8.jar from Oracle OJDBC 18.3:
https://www.oracle.com/database/technologies/appdev/jdbc-ucp-183-downloads.html

Related

SQLite driver is not suitable

I'm trying to connect to database with SQLite. There is no error appears in editor, but when I run the application I am getting error message that says;
"java.sql.SQLException: No suitable driver found for jdbc:sqlite//school.sqlite"
package dbUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbConnection {
private static final String SQCONN = "jdbc:sqlite//school.sqlite";
public static Connection getConnection() throws SQLException {
try {
Class.forName("org.sqlite.JDBC");
return DriverManager.getConnection(SQCONN);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
}
I did download the latest sqlite driver to library which is "sqlite-jdbc-3.23.1"
Can anybody help me for this error message? Thankyou,
If anybody sees this post and having the same problem, I just changed
private static final String SQCONN = "jdbc:sqlite//school.sqlite";
to fullpath;
private static final String SQCONN = "jdbc:sqlite:/C:/Users/MAMI/Desktop/OKUL/SchoolSystem/src/school.sqlite";
as #a_horse_with_no_name mentioned. Works fine now.
Thanks again

Android Connection with Oracle Database

by clicking a button, i want to execute a select query to show some results,i used the Internet permission and the AsyncTask and the address ip of my PC to perform this call but it didn t work for me,Also the Log doesn't show me Anything to figure out the issue,this is the code:
in Manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
Android programm:
package com.ammach.oraclecon;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
class Task extends AsyncTask<Void,Void,String>{
#Override
protected String doInBackground(Void... params) {
String param="mal9a walou";
String driver="oracle.jdbc.driver.OracleDriver";
String bd="ORCL";
String user="SYSTEM";
String passwd="SYSTEM";
String port="1521";
String ip="192.168.1.7";
String url="jdbc:oracle:thin:#"+ip+":"+port+":"+bd;
Connection con=null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, passwd);
Log.e("coooon", "coooon");
Statement st=con.createStatement();
Log.e("Statement", "Statement");
ResultSet resultSet = st.executeQuery("select * from auteur");
Log.e("ResultSet", "ResultSet");
Log.e("con", "You made it, take control your database now!");
if(resultSet.next()){
param=resultSet.getString("nom");
Log.e("dkhal", "rah dkhal");
}else{
Log.e("walou", "walou");
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return param;
}
#Override
protected void onPostExecute(String param) {
super.onPostExecute(param);
textView.setText(param);
Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT).show();
}
}
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.txt);
}
public void fetchOracle(View view) {
new Task().execute();
Toast.makeText(MainActivity.this, "starting", Toast.LENGTH_SHORT).show();
}
}
i Also run the same procedure in a simple java project(void main) in Netbeans, and it works perfectly,i tried so many times the Android application but i failed ,could someone help me to solve this issue.
this is the java programm:
String param = "nothing";
String driver = "oracle.jdbc.driver.OracleDriver";
String bd = "ORCL";
String user = "SYSTEM";
String passwd = "SYSTEM";
String port = "1521";
String ip = "localhost";
String url = "jdbc:oracle:thin:#" + ip + ":" + port + ":" + bd;
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, passwd);
Statement st = con.createStatement();
ResultSet resultSet = st.executeQuery("select * from auteur");
if (resultSet.next()) {
param = resultSet.getString("nom");
} else {
System.out.println("no result");
}
System.out.println("nom: "+param);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
Your catch clause with the only statement e.printStackTrace(); makes the trick. It just swallows the exception and you, of course, unable to see it. It is much better to use the Log class to report the caught exception. Insert there something like Log.e("tag", e.toString()); and you'll see the problem.
Next you should always close all the resources you have used. In your case it's the Connection, the Statement and the ResultSet. The correct place to close them is in your (missed) finally clause.
But anyway, if you want to work with Oracle from Android device then you should ensure the network 192.168.1.* is reachable from your device and the address (192.168.1.7) at least can be pinged successfully (from your device). Have you managed to somehow test the connection? And there can be much more reasons to get an exception (which is silently swallowed in your code), it means the first thing you should do is to change your code and to log exception properly. Next it can be a missing JDBC driver library, it can be a network configuration problem which makes the database unreachable (most probable), it can be the problem on the side of the Oracle if you have no permission to read from "auteur", etc.

Connection to sqlite database [duplicate]

I am using one simple code to access the SQLite database from Java application .
My code is
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectSQLite
{
public static void main(String[] args)
{
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try
{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
while (resultSet.next())
{
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("EMPNAME"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
But this code gives one exception like
java.lang.ClassNotFoundException: org.sqlite.JDBC
How can I slove this,please help me.
You need to have a SQLite JDBC driver in your classpath.
Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don't need to configure them separately.
If you are using netbeans Download the sqlitejdbc driver
Right click the Libraries folder from the Project window and select Add Library ,
then click on the Create button enter the Library name (SQLite) and hit OK
You have to add the sqlitejdbc driver to the class path , click on the
Add Jar/Folder.. button and select the sqlitejdbc file you've downloaded previously
Hit OK and you are ready to go !
If you are using Netbeans using Maven to add library is easier. I have tried using above solutions but it didn't work.
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
</dependencies>
I have added Maven dependency and java.lang.ClassNotFoundException: org.sqlite.JDBC error gone.
I'm using Eclipse and I copied your code and got the same error. I then opened up the project properties->Java Build Path -> Libraries->Add External JARs...
c:\jrun4\lib\sqlitejdbc-v056.jar
Worked like a charm. You may need to restart your web server if you've just copied the .jar file.
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import org.sqlite.SQLiteDataSource;
import org.sqlite.SQLiteJDBCLoader;
public class Test {
public static final boolean Connected() {
boolean initialize = SQLiteJDBCLoader.initialize();
SQLiteDataSource dataSource = new SQLiteDataSource();
dataSource.setUrl("jdbc:sqlite:/home/users.sqlite");
int i=0;
try {
ResultSet executeQuery = dataSource.getConnection()
.createStatement().executeQuery("select * from \"Table\"");
while (executeQuery.next()) {
i++;
System.out.println("out: "+executeQuery.getMetaData().getColumnLabel(i));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
return initialize;
}
You have to download and add the SQLite JDBC driver to your classpath.
You can download from here https://bitbucket.org/xerial/sqlite-jdbc/downloads
If you use Gradle, you will only have to add the SQLite dependency:
dependencies {
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
}
Next thing you have to do is to initialize the driver:
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException eString) {
System.err.println("Could not init JDBC driver - driver not found");
}
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
Instead of this put
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb");
Hey i have posted a video tutorial on youtube about this, you can check that and you can find here the sample code :
http://myfundatimemachine.blogspot.in/2012/06/database-connection-to-java-application.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Connectdatabase {
Connection con = null;
public static Connection ConnecrDb(){
try{
//String dir = System.getProperty("user.dir");
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
return con;
}
catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,"Problem with connection of database");
return null;
}
}
}

Openshift - No suitable driver found for jdbc:mysql

I am new to Java and Servlet Programming. I am trying to host a simple application which is working successfully in localhost. but when i host it to Openshift, it says No suitable driver found for jdbc:mysql://127.12.204.2:3306/shifar .
All i want to do is to save a string into the database.
Here is my code
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String
HOST = System.getenv("OPENSHIFT_MYSQL_DB_HOST"),
PORT = System.getenv("OPENSHIFT_MYSQL_DB_PORT"),
USERNAME = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"),
PASSWORD = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"),
DB_NAME = "shifar";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("userName");
PrintWriter pw = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
pw.println("Error while loading driver: "+e.getMessage());
}
try {
String url = "jdbc:mysql://" + HOST + ":" + PORT +
"/" + DB_NAME;
Connection con = DriverManager.getConnection(url, USERNAME, PASSWORD);
PreparedStatement prep = con.prepareStatement("INSERT INTO names (name) VALUE (?)");
prep.setString(1, name);
int rc = prep.executeUpdate();
pw.println("Name saved !:"+name+" # "+ rc);
} catch (SQLException e) {
pw.println("Error while connecting: "+e.getMessage());
}
}
}
I can't figure out the error :(. The deployment of the application is done through Git as .WAR
Live Preview - (Enter something in the edittext and submit)
Your servlet container needs access to the jar file its way. For instance Tomcat might want something like mysql-connector-java-5.1.35-bin.jar
in the web-inf folder under the application. You need to focus on your classpath and the setup of your servlet container, regardless of what that is.
If you need further assistance hang a question under this with more details.

How to connect SQLite with Java?

I am using one simple code to access the SQLite database from Java application .
My code is
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectSQLite
{
public static void main(String[] args)
{
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try
{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
while (resultSet.next())
{
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("EMPNAME"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
But this code gives one exception like
java.lang.ClassNotFoundException: org.sqlite.JDBC
How can I slove this,please help me.
You need to have a SQLite JDBC driver in your classpath.
Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don't need to configure them separately.
If you are using netbeans Download the sqlitejdbc driver
Right click the Libraries folder from the Project window and select Add Library ,
then click on the Create button enter the Library name (SQLite) and hit OK
You have to add the sqlitejdbc driver to the class path , click on the
Add Jar/Folder.. button and select the sqlitejdbc file you've downloaded previously
Hit OK and you are ready to go !
If you are using Netbeans using Maven to add library is easier. I have tried using above solutions but it didn't work.
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
</dependencies>
I have added Maven dependency and java.lang.ClassNotFoundException: org.sqlite.JDBC error gone.
I'm using Eclipse and I copied your code and got the same error. I then opened up the project properties->Java Build Path -> Libraries->Add External JARs...
c:\jrun4\lib\sqlitejdbc-v056.jar
Worked like a charm. You may need to restart your web server if you've just copied the .jar file.
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import org.sqlite.SQLiteDataSource;
import org.sqlite.SQLiteJDBCLoader;
public class Test {
public static final boolean Connected() {
boolean initialize = SQLiteJDBCLoader.initialize();
SQLiteDataSource dataSource = new SQLiteDataSource();
dataSource.setUrl("jdbc:sqlite:/home/users.sqlite");
int i=0;
try {
ResultSet executeQuery = dataSource.getConnection()
.createStatement().executeQuery("select * from \"Table\"");
while (executeQuery.next()) {
i++;
System.out.println("out: "+executeQuery.getMetaData().getColumnLabel(i));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
return initialize;
}
You have to download and add the SQLite JDBC driver to your classpath.
You can download from here https://bitbucket.org/xerial/sqlite-jdbc/downloads
If you use Gradle, you will only have to add the SQLite dependency:
dependencies {
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
}
Next thing you have to do is to initialize the driver:
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException eString) {
System.err.println("Could not init JDBC driver - driver not found");
}
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
Instead of this put
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb");
Hey i have posted a video tutorial on youtube about this, you can check that and you can find here the sample code :
http://myfundatimemachine.blogspot.in/2012/06/database-connection-to-java-application.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Connectdatabase {
Connection con = null;
public static Connection ConnecrDb(){
try{
//String dir = System.getProperty("user.dir");
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
return con;
}
catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,"Problem with connection of database");
return null;
}
}
}

Categories

Resources