这是我实现的代码,只想插入一个字段而不是其他两个字段 我想使用jdbc方法而不是volley方法在mysql数据库中插入数据 我把这段代码放在doInBackground函数中 当我单击按钮时,它显示错误,但应用程序不崩溃
public class MainActivity extends AppCompatActivity {
EditText email,username,password;
Button insert;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email=(EditText)findViewById(R.id.email);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
insert=(Button)findViewById(R.id.insert);
textView=(TextView)findViewById(R.id.tv);
}
public void insert(View view) {
Send send=new Send();
send.execute("");
}
private class Send extends AsyncTask<String,String,String> {
String msg="";
String Email=email.getText().toString();
// String User=username.getText().toString();
//String Pass=password.getText().toString();
@Override
protected void onPreExecute() {
//super.onPreExecute();
textView.setText("please wait inserting data ");
}
@Override
protected String doInBackground(String... strings) {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Connection con= DriverManager.getConnection("jdbc:mysql://127.0.0.1:81/check","root","");
Connection con=(Connection) DriverManager.getConnection("http://192.168.10.12:81/check","check123","123");
if(con==null)
{
msg="connection goes wrong ";
}
else
{
//String query="insert into registration (id,email,username,password) values ('"+Email+"','"+User+"','"+Pass+"')";
String query="insert into test (name) values ('"+Email+"')";
Statement stmt=con.createStatement();
stmt.executeQuery(query);
msg="insert success ";
}
con.close();
}
catch (Exception e)
{
msg="connection goes wrong ";
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(String s) {
//super.onPostExecute(s);
textView.setText(msg);
}
}
}
我已经包含了jdbc驱动程序编译项目(':mysql-connector-java-3.0.17-ga-bin')
错误由android studio显示
W/System.err: java.sql.SQLException: No suitable driver
W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:186)
W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:213)
W/System.err: at com.example.learnersarena.async.MainActivity$Send.doInBackground(MainActivity.java:56)
W/System.err: at com.example.learnersarena.async.MainActivity$Send.doInBackground(MainActivity.java:37)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)