Android与Oracle数据库的连接(Android Connection with Oracle Database)

编程入门 行业动态 更新时间:2024-10-28 22:24:29
Android与Oracle数据库的连接(Android Connection with Oracle Database)

通过点击一个按钮,我想执行一个选择查询来显示一些结果,我使用Internet权限和AsyncTask和我的PC的地址IP来执行此调用,但它没有为我工作,而且日志不告诉我任何想弄清楚的问题,这是代码:

在清单文件中:

<uses-permission android:name="android.permission.INTERNET"/>

Android程序:

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(); } }

我也在Netbeans的一个简单的Java项目(void main)中运行相同的过程,它完美的工作,我尝试了很多次的Android应用程序,但我失败了,有人可以帮我解决这个问题。

这是Java程序:

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(); }

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(); }

最满意答案

你的catch子句用唯一的语句e.printStackTrace(); 制造诡计。 它只是吞噬了例外,当然,你无法看到它。 使用Log类报告捕获的异常要好得多。 插入那里像Log.e(“tag”,e.toString()); 你会看到问题。

接下来,您应该始终关闭您使用过的所有资源。 在你的情况下,它是连接,语句和ResultSet。 关闭它们的正确位置在你的(错过的)finally子句中。

但无论如何,如果你想从Android设备使用Oracle,那么你应该确保你的设备可以访问网络192.168.1。*,并且地址(192.168.1.7)至少可以成功地从你的设备中ping通。 你设法以某种方式测试连接? 并且可以有更多的理由来获得异常(它被你的代码默默吞下),这意味着你应该做的第一件事就是改变你的代码并正确地登录异常。 接下来,它可能是缺少的JDBC驱动程序库,它可能是一个网络配置问题,导致数据库无法访问(最可能),如果您没有权限从“auteur”读取数据,则可能是Oracle侧的问题,等等

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.

更多推荐

本文发布于:2023-07-25 22:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Oracle   Android   Connection   Database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!