技术标签: java android mysql jdbc 数据库 Android
<uses-permission android:name="android.permission.INTERNET" />
将mysql的驱动包复制到libs当中
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', 0x3133333333333333333333);
INSERT INTO `users` VALUES ('3', '1233', '反复的', '1233', '12', 0x3132333333333333333333);
INSERT INTO `users` VALUES ('4', '1244', '第三代', '1244', '12', 0x3133333333333333333333);
INSERT INTO `users` VALUES ('5', '1255', 'SAS', '1255', '33', 0x3133333333333333333333);
切换会Android视图
注意链接数据库的地址是:jdbc:mysql://10.0.2.2:3306/test
package com.example.myapplication.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn= DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/test","root","root");
}catch (Exception exception){
exception.printStackTrace();
}
return conn;
}
public static void close(Connection conn){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
package com.example.myapplication.entity;
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) {
this.id = id;
this.name = name;
this.username = username;
this.password = password;
this.age = age;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package com.example.myapplication.dao;
import com.example.myapplication.entity.User;
import com.example.myapplication.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public boolean login(String name,String password){
String sql = "select * from users where name = ? and password = ?";
Connection con = JDBCUtils.getConn();
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,name);
pst.setString(2,password);
if(pst.executeQuery().next()){
return true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return false;
}
public boolean register(User user){
String sql = "insert into users(name,username,password,age,phone) values (?,?,?,?,?)";
Connection con = JDBCUtils.getConn();
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,user.getName());
pst.setString(2,user.getUsername());
pst.setString(3,user.getPassword());
pst.setInt(4,user.getAge());
pst.setString(5,user.getPhone());
int value = pst.executeUpdate();
if(value>0){
return true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return false;
}
public User findUser(String name){
String sql = "select * from users where name = ?";
Connection con = JDBCUtils.getConn();
User user = null;
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,name);
ResultSet rs = pst.executeQuery();
while (rs.next()){
int id = rs.getInt(0);
String namedb = rs.getString(1);
String username = rs.getString(2);
String passworddb = rs.getString(3);
int age = rs.getInt(4);
String phone = rs.getString(5);
user = new User(id,namedb,username,passworddb,age,phone);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return user;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<LinearLayout
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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="账号:" />
<EditText
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="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="密码:"
/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<Button
android:layout_marginTop="50dp"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="login"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="reg"
android:text="注册" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
效果
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".RegisterActivity">
<LinearLayout
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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="账号:" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="昵称:" />
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="密码:"
/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="手机:"
/>
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="phone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="年龄:"
/>
<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<Button
android:layout_marginTop="50dp"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:onClick="register"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重置" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.application01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.application01.dao.UserDao;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void reg(View view){
startActivity(new Intent(getApplicationContext(),RegisterActivity.class));
}
public void login(View view){
EditText EditTextname = (EditText)findViewById(R.id.name);
EditText EditTextpassword = (EditText)findViewById(R.id.password);
new Thread(){
@Override
public void run() {
UserDao userDao = new UserDao();
boolean aa = userDao.login(EditTextname.getText().toString(),EditTextpassword.getText().toString());
int msg = 0;
if(aa){
msg = 1;
}
hand1.sendEmptyMessage(msg);
}
}.start();
}
final Handler hand1 = new Handler()
{
@Override
public void handleMessage(Message msg) {
if(msg.what == 1)
{
Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show();
}
}
};
}
package com.example.application01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.application01.dao.UserDao;
import com.example.application01.entity.User;
public class RegisterActivity extends AppCompatActivity {
EditText name = null;
EditText username = null;
EditText password = null;
EditText phone = null;
EditText age = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
name = findViewById(R.id.name);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
phone = findViewById(R.id.phone);
age = findViewById(R.id.age);
}
public void register(View view){
String cname = name.getText().toString();
String cusername = username.getText().toString();
String cpassword = password.getText().toString();
System.out.println(phone.getText().toString());
String cphone = phone.getText().toString();
int cgae = Integer.parseInt(age.getText().toString());
if(cname.length() < 2 || cusername.length() < 2 || cpassword.length() < 2 ){
Toast.makeText(getApplicationContext(),"输入信息不符合要求请重新输入",Toast.LENGTH_LONG).show();
return;
}
User user = new User();
user.setName(cname);
user.setUsername(cusername);
user.setPassword(cpassword);
user.setAge(cgae);
user.setPhone(cphone);
new Thread(){
@Override
public void run() {
int msg = 0;
UserDao userDao = new UserDao();
User uu = userDao.findUser(user.getName());
if(uu != null){
msg = 1;
}
boolean flag = userDao.register(user);
if(flag){
msg = 2;
}
hand.sendEmptyMessage(msg);
}
}.start();
}
final Handler hand = new Handler()
{
@Override
public void handleMessage(Message msg) {
if(msg.what == 0)
{
Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
}
if(msg.what == 1)
{
Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();
}
if(msg.what == 2)
{
//startActivity(new Intent(getApplication(),MainActivity.class));
Intent intent = new Intent();
//将想要传递的数据用putExtra封装在intent中
intent.putExtra("a","註冊");
setResult(RESULT_CANCELED,intent);
finish();
}
}
};
}
该文章是关于对班里学生某门课程成绩进行排序的问题。要求对班里的学生按照成绩从高到低排序输出。输入包括学生数目和每个学生的成绩,输出为按成绩从高到低的排序结果。
文章浏览阅读963次。集群健康度分片健康红:至少有一个主分片没有分配黄:至少有一个副本没有分配绿:主副本分片全部正常分配索引健康:最差的分片的状态集群健康:最差的索引的状态Health 相关的 APIGET _cluster/health集群的状态(检查 节点数量)GET _cluster/health?level=indices所有索引的健康状态 (查看有问题的索引GET _cluster/health/my_index单个索引的健康状态(查看具体的索引)GET _cl_es yellow删除索引
文章浏览阅读559次。A:透明度R:红色G:绿B:蓝Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4,G=4,B=4,那么一个像素点占4+4+4+4=16位 Bitmap.Config ARGB_8888:每个像素占四位,即A=8,R=8,G=8,B=8,那么一个像素点占8+8+8+8=32位Bitmap.Config RGB_565:每个像素占四位,即R=5,G_图片数据像素点占比多少
文章浏览阅读512次。一、修改Ueditor的config.json的文档图片访问路径前缀改成FTP对外暴露的访问地址"imageUrlPrefix": "http://192.168.85.98:8280/up/"二、初始化Ueditor时绑定自定义文件上传方法<!-- 富文本编辑器 --><div id="content"> <script id="edit..._ueditor 上传图片 spring
文章浏览阅读2.2k次。相当部分的 STM32芯片都带USB模块,有时我们会考虑利用STM32芯片的USB模块进行程序代码的下载或升级。USB协议中有专门针对设备固件升级的类协议,即可以通过DFU类协议进行产品固件的加载或更新。 关于STM32产品的DFU程序下载和升级,ST官方有相关的资料文档。可以去www.stmcu.com.cn 或者去www.st.com 搜索DFUse下载相关资料。_syscfg_memoryremapconfig( syscfg_memoryremap_sram )作用是什么;
文章浏览阅读904次。过去,我是在写expect脚本来实现自动登陆并上传下载文件。不过略感不顺。参考文档:http://blog.chinaunix.net/uid-20526681-id-3549245.html现在有一个好的方法cd 到本地你要上传或下载的目录中ftp -niv << EOFopen ip_addressuser username passwordasciiput filen..._shell标本实现ftp
文章浏览阅读388次。星标/置顶小屋,带你解锁最萌最前沿的NLP、搜索与推荐技术文 | 小鹿鹿lulu编 | YY前言由于 BERT-like 模型在 NLP 领域上的成功,研究者们开始尝试将其应用到更为复杂..._bert进行知识推理
文章浏览阅读45次。String 类常用方法注意点:字符串 str 中字符的索引从0开始,范围为 0 到 str.length()-1使用 indexOf 进行字符或字符串查找时,如果匹配返回位置索引;如果没有匹配结果,返回 -1使用 substring(beginIndex , endIndex) 进行字符串截取时,包括 beginIndex 位置的字符,不包括 endIndex 位置的字符“==” ...
文章浏览阅读334次。9月29日,ENVELOP项目在Crypto Horses社区举办了AMA活动,与5万多位社群成员分享了项目进展。项目CEOAlex Shedogubov与大家积极互动交流,一起探讨项目发展与治理。嘉宾及项目介绍Hi there! I am Alex Shedogubov and I am a CEO in ENVELOP. I manage the project and the product development. I have more than 8 years of mana..._envelop上了几个平台
文章浏览阅读3.4w次,点赞26次,收藏131次。Python中的f字符串的用法:要在字符串中插入变量的值,可在前引号前加上字母f,再将要插入的变量放在花括号内。举例子如下:first_name="ada"last_name="lovelace"full_name=f"{first_name}{last_name}"print(f"Hello,{full_name.title()}!")打印结果为:Hello,Ada Lovelace!还可以使用f字符串来创建消息,再把整条消息赋给变量:举例子:first_name=_python f
文章浏览阅读2.9k次。项目需求是需要实现一个垂直方向的跑马灯轮播,早期采用react-native-swiper解决方案,此方案在ios端正常使用,在android端不能使用,所有果断放弃。第二方案打算使用ant-mobile的Carousel组件,import { Carousel, WingBlank } from 'antd-mobile';import { Text } from 'react-nat..._react-native-anchor-carousel 竖向
文章浏览阅读2k次,点赞2次,收藏2次。[I]LK0001[Q]我国现行法律体系中专门针对无线电管理的最高法律文件及其立法机关是:[A]中华人民共和国无线电管理条例,国务院和中央军委[B]中华人民共和国无线电管理办法,工业和信息化部[C]中华人民共和国电信条例,国务院[D]中华人民共和国业余无线电台管理办法,工业和信息化部[P][I]LK0002[Q]我国现行法律体系中专门针对业余无线电台管理的最高法律文件及其立法机关是:[A]业余无线电台管理办法,工业和信息化部[B]个人业余无线电台管理暂行办法,国家体委和国家无委[C]业_a类无线电考试卷