Android连接MySQL数据库(AndroidStudio环境下运行)
2023年7月13日发(作者:)
Android连接MySQL数据库(AndroidStudio环境下运⾏)1.环境配置:(1)Android Studio软件(附带可以运⾏正常测试)(2)Mysql软件
(3)Mysql对应的jar包.2.详细步骤(1)MySQL数据:如下图MySQL库下⾯默认有个user⽤户表,创建⼀个新的⽤户(为了权限的使⽤))MySQL中创建⼀个⽤户并为其设置权限.代码使⽤本机的IP地址连接数据库,不能使⽤localhost进⾏(1)创建⼯程(简单操作)(2)为⼯程导⼊包,⾃⾏操作项⽬结构就这样代码实现:(1)⽂件 注意增加了新的配置数据库的时候⽤着两个(2)布局⽂件,差个图⽚,⾃⼰导⼊就⾏哦 (3)主函数package ication;import ;import ;import ;import ;import nment;import patActivity;import ;import ;import ;import ;import iew;import utton;import ew;import ;import ;import tputStream;import tion;import Manager;import edStatement;import Set;import eption;import ent;import ar;public class MainActivity extends AppCompatActivity implements kListener { 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) { te(savedInstanceState); setContentView(ty_main); imageView=(ImageView)findViewById(iew); textView=(TextView)findViewById(ew); button=(Button)findViewById(); lickListener(this); radioButton1=(RadioButton)findViewById(utton1); radioButton2=(RadioButton)findViewById(utton2); radioButton3=(RadioButton)findViewById(utton3); lickListener(this); lickListener(this); lickListener(this); //为按钮设置⼀个标记 (0); (1); (2); (3); } @Override public void onClick(View v) { Integer id=(Integer)(); if(id==0) { if(id==0) { new Thread(new Runnable() { @Override public void run() { try { e(""); tion cn= nection("jdbc:mysql://192.168.65.99/test","rootes","123456"); String sql="select name from test"; Statement st=(Statement)Statement(); ResultSet rs=eQuery(sql); while(()){ String mybook=ing("name"); n(mybook); Log.i("Mainactivity",mybook); } (); (); (); n("连接数据库成功"); } catch (ClassNotFoundException e) { n("连接数据库失败"); tackTrace(); } catch (SQLException e) { tackTrace(); } } }).start(); //把imageView转换成Bitmap // Bitmap bitmap=loadBitmapFromView(imageView); //获取图像对应的RGB值.,并存⼊数据库中 //GetRGB(bitmap); // conn=getConnection(); //把bitmap转换成为⼀个⽂件 //InputPhoto(bitmap); //显⽰新的Bitmap图⽚ // geBitmap(bitmap); /// n("取像"); }else if(id==1){ n("价格"); }else if(id==2){ n("营养"); }else if(id==3){ n("品种"); } } ///获取图⽚的RGB值 private void GetRGB(Bitmap bitmap) { int w=th(); int h=ght(); StringBuffer sql=new StringBuffer(); ("insert into tb_rgb(x,y,r,g,b,rgb)values "); for(int i=1;i>16)&0xff; int g =( argb>>8)&0xff; int b =argb&0xff; int a =(argb>>24)&0xff; int rgb =(a<<24)|(r<<16)|(g<<8)|b; ("("+j+","+i+","+r+","+j+","+b+","+rgb+"),"); // n("R="+r+",G="+g+",B="+b); el(j,i,); el(j,i,); } } //插⼊数据⼊库 InsertSql(ng()); n("数据库插⼊成功"); } //获取imageView中的图⽚ private Bitmap loadBitmapFromView(ImageView imageView) { wingCacheEnabled(true); wingCacheQuality(G_CACHE_QUALITY_HIGH); wingCacheBackgroundColor(); int w = th(); int h = ght(); Bitmap bmp = Bitmap(w, h, _8888); Canvas canvas = new Canvas(bmp); lor(); //如果不设置canvas的画布为⽩⾊,则⽣成透明 (0, 0, w, h); (canvas); return bmp; } //连接数据库 public Connection getConnection(){ try{ e(""); conn= nection(url, user, password); n("数据库连接成功"); } catch (Exception e) { n("数据库连接失败"); tackTrace(); } return conn; } public void InsertSql(String sql){ String sqls=ing(0,()-1); conn=getConnection();//连接数据库 n("连接数据库成功"); try{ stat=Statement(); //e(sqls); }catch (Exception e){ n("数据库插⼊失败"); tackTrace(); } } //保存图⽚⽂件 private void InputPhoto(Bitmap bitmap) { FileOutputStream fos; String imagePath=""; try { boolean isHasSDCard = ernalStorageState().equals( _MOUNTED); if (isHasSDCard) { File sdRoot = ernalStorageDirectory(); File file = new File(sdRoot, tance(). getTimeInMillis() + ".png"); n("⽂件路径"+ng()); fos = new FileOutputStream(file); imagePath = olutePath(); }else { throw new Exception("创建⽂件夹失败了"); } ss(,90,fos); (); (); }catch (Exception e) { n("⽂件保存失败"); tackTrace(); } yDrawingCache(); n("保存⽂件成功"); }}注意MySQL的连接操作必须放在⼦线程中才可以执⾏,不然运⾏不了,为什么原因暂时不知道,望⾼⼈指点⼀⼆选择连接的⽤户必须选择刚才创建的有权限很⾼的⽤户,不然不可以哦.运⾏结果
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689215581a222213.html
评论列表(0条)