Android studio 连接数据库问题 我这样写有错吗帮我看看

package com.example.administrator.testdate;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {
String Username="root"; //数据库用户名
String Password="12345678"; //数据库用户密码
Button button1;
Button button2;
EditText textView1;
EditText textView2;
Connection conn=null;
Statement stmt=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1=(EditText)findViewById(R.id.editText2);
textView2=(EditText)findViewById(R.id.editText) ;
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }

    });
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            connection();

        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView1.setText("");
            textView2.setText("");

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void connection()
{
    String username = textView1.getText().toString();
    String password=textView2.getText().toString();
    if (username != null) {
        //打开数据库
        try {//注册驱动程序
            Class.forName("com.mysql.jdbc.Driver");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.println("Driver Error " + e.getMessage());
        }
        //连接数据库并创建Statement 对象
        String sConnStr = "jdbc:mysql://localhost/testdate?user=" + Username + "&password=" + Password + "&useUnicode=true&characterEncoding=utf8";
        try {
            conn = DriverManager.getConnection(sConnStr);
            conn.setAutoCommit(true);
            //更新数据库
            String sql = "insert into testdate(username,password) values(?,?)";
            PreparedStatement sta = conn.prepareStatement(sql);
            sta.setString(1, username);
            sta.setString(2, password);
            sta.executeUpdate();
            sta.close();
            conn.close();


        } catch (Exception e) {
            System.err.println("数据库连接错误: " + e.getMessage());
            return;
        }

    }
}

}

4个回答

android可以连接mysql?不是一般都是连接sqlite吗?

参考:http://blog.csdn.net/liuhe688/article/details/6715983/

楼上
Android怎么会不能连接MySQL
都支持

我没有在Android studio这样写过,直接在studio写连接数据库的代码没试过。
下面用的是在Android通过http协议传输数据到java后端,数据库还是在java后端处理的。处理后的结果返回给studio
http://blog.csdn.net/niaonao/article/details/52210651

都是写在java后端的 这个没试过

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问