Android+MySQL

Android Studio或Eclipse IDE中
1.网络访问权限,当手机需要访问网络,则需要在配置文件(AndroidManifest.xml)中添加该权限。

<uses-permission android:name="android.permission.INTERNET"/>

2.java代码

public class MainActivity extends Activity implements OnClickListener{
	//(10.0.2.2)该IP是android模拟器中宿主机的ip地址、mydb为数据库名
	private String url = "jdbc:mysql://10.0.2.2:3306/mydb";
	private String user = "root";
	private String pass = "123456";
	
	private Button login;
	private Button zhuce;
	private EditText etUser;
	private EditText etPass;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		login = (Button) findViewById(R.id.login);
		etPass = (EditText) findViewById(R.id.et_pass);
		login.setOnClickListener(this);
	}
	
	//连接数据库等操做需在子线程中调用
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		new Thread(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Class.forName("org.gjt.mm.mysql.Driver");
					System.out.println("成功加载MySQL驱动!");
					Connection conn;
					conn = (Connection) DriverManager.getConnection(url, user, pass);
					System.out.println("成功连接到数据库!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					System.out.println("连接数据库失败!");
					e.printStackTrace();
				}
			}
		}.start();	
	}
}

3.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0FF"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:" />

        <EditText
            android:id="@+id/et_user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:hint="请输入账号" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:" />

        <EditText
            android:id="@+id/et_pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:singleLine="true"
            android:hint="请输入密码" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆" />

        <Button
            android:id="@+id/zuce"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册" />
    </LinearLayout>
</LinearLayout>

4.加载jar包
需驱动MySql的jar包,该jar包先放在libs文件中,然后在将该jar包加载到Libraries库中
https://download.csdn.net/download/yfcjava/11727718


Mysql数据库查询方式(对数据库的操作都要放到线程中去)

#循环遍历查询
	String sql = "select * from user";
	Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = (Statement) conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                                if (userName.equals(rs.getString("user"))
                                        && passWord.equals(rs.getString("pass"))) {
                                    System.out.println("登陆成功");
                                    startActivity(new Intent(Main.this, HomeActivity.class));
                                } else {
                                    System.out.println("密码错误");
                                }
        } catch (Exception e) {
            ;
        }
        

上传图片到Mysql

//获取drawable下的图片
//是获取项目中的资源文件可以用来获取你说的string,xml还可以获取图片,音乐,视频等资源文件。
        Resources res = getResources();
        BitmapDrawable d = (BitmapDrawable) res.getDrawable(R.drawable.ys);
        Bitmap img = d.getBitmap();

        String fn = "ys.bmp";
        //File.separator 的作用相当于 ' \  '、用于获取/data/data/files目录
        String path = getFilesDir() + File.separator + fn;
        try{
            //创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
            OutputStream os = new FileOutputStream(path);
            //图像压缩的方法,三个参数分别是压缩后的图像的格式(png),图像显示的质量(0—100),
            // 100表示最高质量,图像处理的输出流(out)。
            img.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.close();
        }catch(Exception e){
            Log.e("TAG", "", e);
        }

        file = new File(path);
上传图片到mysql
public class SQLUtil {
	public static void blobInsert(Connection conn, File file) {
	        PreparedStatement ps = null;
	        FileInputStream in = null;
	        try {
	            in = new FileInputStream(file);
	            // String sql = "INSERT INTO `mydb`.`file` (`file`) VALUES ('" + in
	            // + "');";
	            ps = (PreparedStatement)conn.prepareStatement("insert into mydb.file values(?)");
	            ps.setBinaryStream(1, in, in.available());
	            int count = ps.executeUpdate();
	            if (count > 0) {
	                System.out.println("插入成功!");
	            } else {
	                System.out.println("插入失败!");
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }
    }
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值