What is the best way to connect between android and oracle database?

前端 未结 7 550
后悔当初
后悔当初 2020-12-09 19:48

I need to access data from an external oracle database from my application android to update the local database application, but I don\'t know what would be the best way to

7条回答
  •  佛祖请我去吃肉
    2020-12-09 20:02

    ORACLE DATABASE CONNECTION WITH ANDROID THROUGH LAN

    Grant Some Manifest Permissions

    
    
        
        
        
        
        
        
        
    

    MainActivity Class

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

    Prerequisite are: Note there is no need to add dependency lib ojdbc14.jar just copy ojdbc14.jar to your JAVA_HOME jre -> lib -> ext & paste here ojdbc14.jar then first manually check jdbc connection by cmd/terminal make any simple java program http://www.javatpoint.com/example-to-connect-to-the-oracle-database

提交回复
热议问题