尝试从android studio连接到oracle

时间:2019-01-07 11:29:55

标签: java android oracle

我一直试图将我的orcale附加到我的android studio项目中!但没有运气!到目前为止,这是我尝试过的!任何帮助将不胜感激!谢谢!

   //region Description 

    package com.example.ubada.d;

    import android.os.Bundle; import android.os.StrictMode; import
    android.support.v7.app.AppCompatActivity; 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 {
        public static  String DEFAULT_DRIVER =  "oracle.jdbc.driver.OracleDriver";
        public static  String DEFAULT_URL =  "jdbc:oracle:thin:@LAPTOP-22HRGAR3:1522:XE";
        public static  String DEFAULT_USERNAME = "system";
        public static  String DEFAULT_PASSWORD = "system";
        public static Connection connection;    public static Statement stmt;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
              StrictMode.ThreadPolicy policy = new  StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
               TextView tv = findViewById(R.id.tv1);
               try {
                  connection = createConnection();
                  tv.setText("Connected");
                   stmt=connection.createStatement();
                  StringBuffer stringBuffer = new StringBuffer();
                   ResultSet rs=stmt.executeQuery ( "SELECT * FROM " + "SYSTEM"+"."+"OO;");
                     while(rs.next()) {
                    stringBuffer.append(rs.getString(1)).append("\n");
                }
                  Toast.makeText(getApplicationContext(), "conn"+stringBuffer.toString(), Toast.LENGTH_LONG).show();
                   connection.close();
            } 
              catch (Exception e) {
                tv.setText("errrrror"+e.toString());
            }
        }

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

            }


     } //endregion``

0 个答案:

没有答案