有一段时间没有更新博客了,以前小编都是发布有关spring boot的博客,今天换换口味,为大家带来安卓的Demo。
下载地址https://download.csdn.net/download/weixin_43055096/11049453
1:下载安装并配置Andriod studio
2:准备好mysql-connector-java-5.1.47.jar,这个驱动包网上都能下载
3:创建一个空的Activity
1.导入mysql-connector-java-5.1.47.jar包 然后build library(其他的包可以不用管)
2.创建JdbcUtil
package com.wuge.dbtext;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class JdbcUtil {
private static JdbcUtil instance;
public static JdbcUtil getInstance(){
if (instance ==null){
instance = new JdbcUtil();
}
return instance;
}
public Connection getConnection(String dbName,String name,String password) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.43.104:3306/"+dbName;
return DriverManager.getConnection(url,name,password);
} catch (Exception e) {
return null;
}
}
public Connection getConnection(String file){
File f = new File(file);
if(!f.exists()){
return null;
}else {
Properties pro = new Properties();
try {
Class.forName("com.mysql.jdbc.Driver");
pro.load(new FileInputStream(f));
String url = pro.getProperty("url");
String name = pro.getProperty("name");
String password = pro.getProperty("password");
return DriverManager.getConnection(url,name,password);
}catch (Exception e){
return null;
}
}
}
}
1.这里的url是会动态变化的,他是根据你连接的网络不同而改变的,如何查找呢,下图给出查找的方式。
我们直接在控制台输入ipconfig ,红色箭头的地方就是我们需要的ip地址
3.创建 UserDao
package com.wuge.dbtext;
import android.util.Log;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static android.content.ContentValues.TAG;
public class UserDao {
JdbcUtil jdbcUtil = JdbcUtil.getInstance();
//第一个参数为数据库名称,第二个参数为数据库账号 第三个参数为数据库密码
Connection conn = jdbcUtil.getConnection("Dbtext","root","password");
//注册
public boolean register(String name,String password){
if (conn==null){
Log.i(TAG,"register:conn is null");
return false;
}else {
//进行数据库操作
String sql = "insert into user(name,password) values(?,?)";
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1,name);
pre.setString(2,password);
return pre.execute();
} catch (SQLException e) {
return false;
}finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//登录
public boolean login(String name,String password){
if (conn==null){
Log.i(TAG,"register:conn is null");
return false;
}else {
String sql = "select * from user where name=? and password=?";
try {
PreparedStatement pres = conn.prepareStatement(sql);
pres.setString(1,name);
pres.setString(2,password);
ResultSet res = pres.executeQuery();
boolean t = res.next();
return t;
} catch (SQLException e) {
return false;
}
}
}
}
3.完成咋们的xml 小编这里做的很简陋
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="请输入密码"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入用户名"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fun"
android:text="注册"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fun"
android:text="登录"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
</android.support.constraint.ConstraintLayout >
package com.wuge.dbtext;
import android.content.Intent;
import android.os.Looper;
import android.os.Trace;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.gson.JsonObject;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
private EditText name;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
password = findViewById(R.id.password);
}
//用户根据点击事件来找到相应的功能
public void fun(View v){
switch (v.getId()){
case R.id.register:
new Thread(new Runnable() {
@Override
public void run() {
String n = name.getText().toString().trim();
String psw = password.getText().toString().trim();
UserDao ud = new UserDao();
boolean result =ud.register(n,psw);
if (!result){
Looper.prepare();
Toast toast = Toast.makeText(MainActivity.this,"注册成功!",Toast.LENGTH_SHORT);
toast.show();
Looper.loop();
}
Log.i(TAG,"fun"+result);
//以上为jdbc注册
}
}).start();
break;
case R.id.login:
new Thread(new Runnable() {
@Override
public void run() {
String n = name.getText().toString().trim();
String psw = password.getText().toString().trim();
if (n.equals("")||psw.equals("")){
Looper.prepare();
Toast toast = Toast.makeText(MainActivity.this,"输入不能为空!",Toast.LENGTH_SHORT);
toast.show();
Looper.loop();
}
UserDao ud = new UserDao();
Boolean result = ud.login(n,psw);
if (!result){
Looper.prepare();
Toast toast=Toast.makeText(MainActivity.this,"用户名不存在或密码错误!",Toast.LENGTH_SHORT);
toast.show();
Looper.loop();
}else{
Looper.prepare();
Toast toast=Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT);
toast.show();
//一下代码为跳转界面
// Intent intent=new Intent(MainActivity.this,info.class);
//intent.putExtra("name",n);
// startActivity(intent);
Looper.loop();
}
//以上为jdbc登录
}
}).start();
}
}
}
小编这里用到了线程 ,想告诉大家的是 ,所有的Http请求都要写在子线程里面,而且如果想在子线程里面修改UI线程,比如我们的Toast 我们就要引入
Looper.prepare(); Looper.loop();
2.在Activity 可用runUiThread
3.Handler
小编最推荐的是第三种 第二种有局限 只能在同一个Activity上面使用