СОЕДИНЕНИЕ БАЗЫ ДАННЫХ ORACLE С ANDROID ЧЕРЕЗ LAN
Предоставление некоторых разрешений для манифеста
<permission
android:name="info.android.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="info.android.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
Класс MainActivity
package example.com.myapplication;
import java.sql.Connection;
import java.sql.DriverManager;
import android.os.StrictMode;
public class MainActivity extends AppCompatActivity {
private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DEFAULT_URL = "jdbc:oracle:thin:@192.168.0.1:1521:xe";
private static final String DEFAULT_USERNAME = "system";
private static final String DEFAULT_PASSWORD = "oracle";
private Connection connection;
@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);
}
TextView tv = (TextView) findViewById(R.id.hello);
try {
this.connection = createConnection();
e.Log("Connected");
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("select * from cat");
while(rs.next()) {
System.out.println("hello : " + rs.getString(1));
}
connection.close();
}
catch (Exception e) {
e.Log(""+e);
e.printStackTrace();
}
}
public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public static Connection createConnection() throws ClassNotFoundException, SQLException {
return createConnection(DEFAULT_DRIVER, DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
}
}
Предварительные условия: обратите внимание, что нет необходимости добавлять зависимость lib ojdbc14.jar, просто скопируйте ojdbc14.jar в свой JAVA_HOME jre -> lib -> ext & paste сюда ojdbc14.jar, а затем сначала вручную проверьте соединение jdbc с помощью cmd / терминала, сделав любойпростая Java-программа http://www.javatpoint.com/example-to-connect-to-the-oracle-database