android连接数据库(使用oracle直接连接)


1.首先导入ojdbc14.jar包

2.这是首页面

package com.android.logins;

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



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
    EditText userName;
    EditText password;
    Button btn_login;
    String userNameValue, passwordValue;
    Connection connection = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        userName = (EditText) findViewById(R.id.et_zh);
        password = (EditText) findViewById(R.id.et_mima);
        btn_login = (Button) findViewById(R.id.btn_login);

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@192.168.100.109:1521:orcl", "show",
                    "show");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("加载程序驱动出错");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        btn_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try {
                    String sql = "select * from userlogin ";
                    Statement stmt = connection.createStatement();
                    ResultSet rSet = stmt.executeQuery(sql);
                    userNameValue = userName.getText().toString();
                    passwordValue = password.getText().toString();
                    while (rSet.next()) {
                        System.out.println(rSet.getString("name"));
                        System.out.println(rSet.getString("password"));
                        if (userNameValue.equals(rSet.getString("name"))
                                && passwordValue.equals(rSet
                                        .getString("password"))) {
                            Toast.makeText(LoginActivity.this, "登录成功",
                                    Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this,
                                    WelcomeAvtivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(LoginActivity.this,
                                    "用户名或密码错误,请重新登录", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                    rSet.close();
                    stmt.close();
                } catch (Exception e) {
                    System.out.println(e.getMessage().toString());
                } finally {
                    if (connection != null) {
                        try {
                            connection.close();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

    }

}

package com.android.logins; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity { /** Called when the activity is first created. */ EditText userName; EditText password; Button btn_login; String userNameValue, passwordValue; Connection connection = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); userName = (EditText) findViewById(R.id.et_zh); password = (EditText) findViewById(R.id.et_mima); btn_login = (Button) findViewById(R.id.btn_login); try { Class.forName("oracle.jdbc.driver.OracleDriver"); connection = DriverManager.getConnection( "jdbc:oracle:thin:@192.168.100.109:1521:orcl", "show", "show"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("加载程序驱动出错"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } btn_login.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { String sql = "select * from userlogin "; Statement stmt = connection.createStatement(); ResultSet rSet = stmt.executeQuery(sql); userNameValue = userName.getText().toString(); passwordValue = password.getText().toString(); while (rSet.next()) { System.out.println(rSet.getString("name")); System.out.println(rSet.getString("password")); if (userNameValue.equals(rSet.getString("name")) && passwordValue.equals(rSet .getString("password"))) { Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, WelcomeAvtivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录", Toast.LENGTH_SHORT) .show(); } } rSet.close(); stmt.close(); } catch (Exception e) { System.out.println(e.getMessage().toString()); } finally { if (connection != null) { try { connection.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }); } }

3.注意AndroidManifest.xml的文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.logins"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LoginActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     由于由跳转,所以在这里必须写,注意,以后如果在跳转页面中使用到,在这里都必须定义   <activity android:name=".WelcomeAvtivity"></activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />
   定义权限,连接网络,这里必须写 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

由于由跳转,所以在这里必须写,注意,以后如果在跳转页面中使用到,在这里都必须定义 定义权限,连接网络,这里必须写

以上是我做的一个基础的连接数据库,由于本人现在也是初学者,如有不足之处,请大家指出,相互学习

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/290842.html

(0)
上一篇 2022年10月10日
下一篇 2022年10月10日

相关推荐

发表回复

登录后才能评论