Java Android studio连接到远程数据库
我正在尝试让Android Studio应用程序连接到托管在“hostbuddy”上的远程服务器。下面是代码(当发送数据时,错误是“连接出错2”),让我猜测DB_URL有问题。我尝试了很多方法来更改url,但都没有成功。如果有帮助的话,我也可以发布日志 编辑:我修复了它,我忘了为MySQl的连接器添加模块,现在工作起来很有魅力^ u^Java Android studio连接到远程数据库,java,sql,android-studio,Java,Sql,Android Studio,我正在尝试让Android Studio应用程序连接到托管在“hostbuddy”上的远程服务器。下面是代码(当发送数据时,错误是“连接出错2”),让我猜测DB_URL有问题。我尝试了很多方法来更改url,但都没有成功。如果有帮助的话,我也可以发布日志 编辑:我修复了它,我忘了为MySQl的连接器添加模块,现在工作起来很有魅力^ u^ package com.example.battl.qr2; import android.content.Intent; import android.os.
package com.example.battl.qr2;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Register extends AppCompatActivity {
TextView textView;
Button button;
EditText editText;
private static final String DB_URL= "jdbc:mysql://mysql6002.site4now.net/db_a37d85_mydb:3306";
private static final String USER = "X";
private static final String PASS = "X";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
textView=(TextView) findViewById(R.id.textView);
editText=(EditText) findViewById(R.id.editText);
}
public void btnConn(View view)
{
Send objsend = new Send();
objsend.execute("");
}
private class Send extends AsyncTask<String,String,String>
{
String msg = "";
String text = editText.getText().toString();
@Override
protected void onPreExecute() {textView.setText("Please Wait Inserting");}
@Override
protected String doInBackground(String... strings)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
if(conn == null) {
msg="Connection goes wrong";
} else {
String query = "INSERT INTO ex (col1) VALUES ('"+text+"') ";
Statement stmt = conn.createStatement();
stmt.executeUpdate(query);
msg = "Inserted successfully";
}
conn.close();
}
catch (Exception e)
{
msg="Connection goes wrong 2";
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {textView.setText(msg);}
}
}
package com.example.battl.qr2;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.w3c.dom.Text;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.Statement;
公共类注册扩展了AppCompatActivity{
文本视图文本视图;
按钮;
编辑文本编辑文本;
私有静态最终字符串DB_URL=“jdbc:mysql://mysql6002.site4now.net/db_a37d85_mydb:3306";
私有静态最终字符串USER=“X”;
私有静态最终字符串传递=“X”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u寄存器);
textView=(textView)findViewById(R.id.textView);
editText=(editText)findViewById(R.id.editText);
}
公共无效BTNCON(视图)
{
Send objsend=new Send();
objsend.execute(“”);
}
私有类发送扩展异步任务
{
字符串msg=“”;
String text=editText.getText().toString();
@凌驾
受保护的void onPreExecute(){textView.setText(“请稍候插入”);}
@凌驾
受保护的字符串背景(字符串…字符串)
{
尝试
{
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn=DriverManager.getConnection(DB_URL,USER,PASS);
如果(conn==null){
msg=“连接出错”;
}否则{
String query=“插入ex(col1)值(“+text+”)”;
语句stmt=conn.createStatement();
stmt.executeUpdate(查询);
msg=“插入成功”;
}
康涅狄格州关闭();
}
捕获(例外e)
{
msg=“连接出错2”;
e、 printStackTrace();
}
返回味精;
}
@凌驾
受保护的void onPostExecute(字符串msg){textView.setText(msg);}
}
}