Android连接MySQL数据库(Android Studio环境下运行)

您所在的位置:网站首页 mybit平台怎么样 Android连接MySQL数据库(Android Studio环境下运行)

Android连接MySQL数据库(Android Studio环境下运行)

2024-07-13 22:40:29| 来源: 网络整理| 查看: 265

1.环境配置:

(1)Android Studio软件(附带可以运行正常测试)

(2)Mysql软件  

(3)Mysql对应的jar包.

2.详细步骤

(1)MySQL数据:如下图

MySQL库下面默认有个user用户表,创建一个新的用户(为了权限的使用))

MySQL中创建一个用户并为其设置权限.

代码使用本机的IP地址连接数据库,不能使用localhost进行

(1)创建工程(简单操作)

(2)为工程导入mysql.jar包,自行操作

项目结构就这样

代码实现:

(1)AndroidManifest.xml文件

注意增加了新的配置

数据库的时候用着两个

(2)布局文件,差个图片,自己导入就行哦

(3)主函数

package com.example.chen.myapplication; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.TextView; import org.w3c.dom.Text; import java.io.File; import java.io.FileOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Calendar; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ImageView imageView; private TextView textView; private Button button; private RadioButton radioButton1; private RadioButton radioButton2; private RadioButton radioButton3; //连接数据库 private String url="jdbc:mysql://192.168.65.99:3306/test"; private String user="rootes"; private String password="123456"; PreparedStatement statement=null; Statement stat=null; Connection conn=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=(ImageView)findViewById(R.id.imageView); textView=(TextView)findViewById(R.id.textView); button=(Button)findViewById(R.id.button); button.setOnClickListener(this); radioButton1=(RadioButton)findViewById(R.id.radioButton1); radioButton2=(RadioButton)findViewById(R.id.radioButton2); radioButton3=(RadioButton)findViewById(R.id.radioButton3); radioButton1.setOnClickListener(this); radioButton2.setOnClickListener(this); radioButton3.setOnClickListener(this); //为按钮设置一个标记 button.setTag(0); radioButton1.setTag(1); radioButton2.setTag(2); radioButton3.setTag(3); } @Override public void onClick(View v) { Integer id=(Integer)v.getTag(); if(id==0) { new Thread(new Runnable() { @Override public void run() { try { Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection cn= DriverManager.getConnection("jdbc:mysql://192.168.65.99/test","rootes","123456"); String sql="select name from test"; Statement st=(Statement)cn.createStatement(); ResultSet rs=st.executeQuery(sql); while(rs.next()){ String mybook=rs.getString("name"); System.out.println(mybook); Log.i("Mainactivity",mybook); } cn.close(); st.close(); rs.close(); System.out.println("连接数据库成功"); } catch (ClassNotFoundException e) { System.out.println("连接数据库失败"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }).start(); //把imageView转换成Bitmap // Bitmap bitmap=loadBitmapFromView(imageView); //获取图像对应的RGB值.,并存入数据库中 //GetRGB(bitmap); // conn=getConnection(); //把bitmap转换成为一个文件 //InputPhoto(bitmap); //显示新的Bitmap图片 // imageView.setImageBitmap(bitmap); /// System.out.println("取像"); }else if(id==1){ System.out.println("价格"); }else if(id==2){ System.out.println("营养"); }else if(id==3){ System.out.println("品种"); } } ///获取图片的RGB值 private void GetRGB(Bitmap bitmap) { int w=bitmap.getWidth(); int h=bitmap.getHeight(); StringBuffer sql=new StringBuffer(); sql.append("insert into tb_rgb(x,y,r,g,b,rgb)values "); for(int i=1;i16)&0xff; int g =( argb>>8)&0xff; int b =argb&0xff; int a =(argb>>24)&0xff; int rgb =(a


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭