2024年4月25日发(作者:)

三、编写数据库和dao以及JDBC相关代码

1、在数据库当中创建表

SQL语句

/*

Navicat MySQL Data Transfer

Source Server : localhost_3306

Source Server Version : 50562

Source Host : localhost:3306

Source Database : test

Target Server Type : MYSQL

Target Server Version : 50562

File Encoding : 65001

Date: 2021-05-10 17:28:36

*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for `student`

-- ----------------------------

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (

`sid` int(11) NOT NULL AUTO_INCREMENT,

`sname` varchar(255) NOT NULL,

`sage` int(11) NOT NULL,

`address` varchar(255) NOT NULL,

PRIMARY KEY (`sid`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of student

-- ----------------------------

INSERT INTO `student` VALUES ('1', 'andi', '21', '21212');

INSERT INTO `student` VALUES ('2', 'a', '2121', '2121');

-- ----------------------------

-- Table structure for `users`

-- ----------------------------

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (

`uid` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(255) NOT NULL,

`username` varchar(255) NOT NULL,

`password` varchar(255) NOT NULL,

`age` int(255) NOT NULL,

`phone` longblob NOT NULL,

PRIMARY KEY (`uid`)

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of users

-- ----------------------------

INSERT INTO `users` VALUES ('2', '123', 'HBV环保局', '123', '33', 0x33333333333);

INSERT INTO `users` VALUES ('3', '1233', '反复的', '1233', '12', 0x33333333333);

INSERT INTO `users` VALUES ('4', '1244', '第三代', '1244', '12', 0x33333333333);

INSERT INTO `users` VALUES ('5', '1255', 'SAS', '1255', '33', 0x33333333333);

2、在Android Studio当中创建JDBCUtils类

切换会Android视图

注意链接数据库的地址是:jdbc:mysql://10.0.2.2:3306/test

package ;

import tion;

import Manager;

import eption;

public class JDBCUtils {

static {

try {

e("");

} catch (ClassNotFoundException e) {

tackTrace();

}

}

public static Connection getConn() {

Connection conn = null;

try {

conn= nection("jdbc:mysql://10.0.2.2:3306/test","root","root");

}catch (Exception exception){

tackTrace();

}

return conn;

}

public static void close(Connection conn){

try {

();

} catch (SQLException throwables) {

tackTrace();

}

}

}

3、创建User实体类

package ;

public class User {

private int id;

private String name;

private String username;

private String password;

private int age;

private String phone;

public User() {

}

public User(int id, String name, String username, String password, int age, String phone) {

= id;

= name;

me = username;

rd = password;

= age;

= phone;

}

public int getId() {

return id;

}

public void setId(int id) {

= id;

}

public String getName() {

return name;

}

public void setName(String name) {

= name;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

me = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

rd = password;

}

public int getAge() {

return age;

}

public void setAge(int age) {

= age;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

= phone;

}

}

4、创建dao层和UserDao

package ;

import ;

import ils;

import tion;

import edStatement;

import Set;

import eption;

public class UserDao {

public boolean login(String name,String password){

String sql = "select * from users where name = ? and password = ?";

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:layout_editor_absoluteX="219dp"

tools:layout_editor_absoluteY="207dp"

android:padding="50dp"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="账号:" />

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

android:text="" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="密码:"

/>

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:layout_editor_absoluteX="219dp"

tools:layout_editor_absoluteY="207dp"

android:padding="50dp"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="账号:" />

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

android:text="" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="密码:"

/>

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_marginTop="50dp"

android:id="@+id/button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录"

android:onClick="login"

/>

android:id="@+id/button3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:onClick="reg"

android:text="注册" />

3、完善MainActivity

package ation01;

import patActivity;

import ;

import ;

import r;

import e;

import ;

import xt;

import ;

import o;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

setContentView(ty_main);

}

public void reg(View view){

startActivity(new Intent(getApplicationContext(),));

}

public void login(View view){

EditText EditTextname = (EditText)findViewById();

EditText EditTextpassword = (EditText)findViewById(rd);

new Thread(){

@Override

public void run() {

UserDao userDao = new UserDao();

boolean aa = (t().toString(),t().toString());

int msg = 0;

if(aa){

msg = 1;

}

ptyMessage(msg);

}

}.start();

}

final Handler hand1 = new Handler()

{

@Override

public void handleMessage(Message msg) {

if( == 1)

{

xt(getApplicationContext(),"登录成功",_LONG).show();

}