Saturday, February 13, 2010

Android/ JDBC

Hi,

This week i was able to develop an Android App to get data from an Oracle Database.

Here is my Code .


package com.prem.JDBCAndroid;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class JDBCAndroid extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

try {
String userName = getDataFromOraDB();
TextView tv = new TextView(this);
tv.setText(userName);
setContentView(tv);
} catch (SQLException e) {
Toast.makeText(this, e.getMessage(), 1).show();
} catch (ClassNotFoundException e) {
Toast.makeText(this, e.getMessage(), 1).show();
}

}

public String getDataFromOraDB() throws SQLException,
ClassNotFoundException {

String name = null;
String jdbcURL = "jdbc:oracle:thin:@hostname:portname:sid";
String user = "uname";
String passwd = "pwd";
// Load the Oracle JDBC driver

try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn;
ResultSet rs;
Statement stmt;
conn = DriverManager.getConnection(jdbcURL, user, passwd);
stmt = conn.createStatement();
rs = stmt.executeQuery("select USERNAME from SomeTableName");
if (rs.next()) {
name = rs.getString("USERNAME");
}
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
System.out.println("the exception is" + e.toString());
}

Toast.makeText(getApplicationContext(), name, 1).show();
return name;
}

}





If you still have issues please check your AndroidManifest.xml file if it has the

below line

uses-permission android:name="android.permission.INTERNET"

5 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

hi i'm a korean...
I have a question. please help me
i meet the message(java.lang.ClassNotFoundException) from android logCat.
what is the problem? i use devlop android2.1, oracle 8.1.7
thanks for your information

Unknown said...

Hello from Spain!
Does this application only works at SDK 2.3?
In Sdk 2.1 I have the error
"oracle cannot be resolved to a type"
in line
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

Any idea?

Danilo Peixoto said...

Hi, I'm getting the same error ervilla reported, I believe it's because I don't have the jdbc driver for oracle installed, could you post where I can get it?

Thanks!

abdou007 said...

where is driver for JDBC oracle i have a probleme
Conversion to Dalvik format failed with error &