我正在尝试与通过我的网站托管的远程mysql数据库建立连接。
我正在使用mysql Connector j v3.0.17。 我将文件添加到libs文件夹,右键单击libs文件夹 - >构建路径 - >配置构建路径。在库下我选择了Add Jars。
然后单击确定。 操作系统:Windows 7
以下是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Go to main menu when the Login button is pressed.
Button goToMainMenuButton = (Button) findViewById(R.id.button_login);
goToMainMenuButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (getConnection() != null)
{
Intent mainScreen = new Intent(arg0.getContext(), MainMenu.class);
startActivity(mainScreen);
}
}
});
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Log.e("Success", "Loaded");
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
Log.e("InstantiationException", e1.toString());
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
Log.e("IllegalAccessException", e1.toString());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
Log.e("ClassNotFoundException", e1.toString());
}
}
public Connection getConnection() throws SQLException {
Connection conn = null;
EditText username = (EditText)findViewById(R.id.username);
EditText password = (EditText)findViewById(R.id.password);
try
{
Properties connectionProps = new Properties();
connectionProps.put("user", username.getText().toString());
connectionProps.put("password", password.getText().toString());
conn = DriverManager.getConnection("jdbc:mysql://website.com:3306/db?user=blah&password=blah");
//conn = DriverManager.getConnection("jdbc:mysql://website.com:3306/db", "blah", "blah");
//conn = DriverManager.getConnection("jdbc:mysql://website.com:3306/db", connectionProps);
} catch (java.sql.SQLException SQLerror) {
Log.e(SQLerror.toString(), username.getText().toString() + " " + password.getText().toString());
}
return conn;
}
对于我的输出,我没有抛出任何错误,我得到的唯一“错误”消息来自此行Log.e(“成功”,“已加载”);它只是告诉我Class.forName("com.mysql.jdbc.Driver").newInstance();
已加载
我想问一下正确的jdbc连接字符串是什么,因为我已经尝试了上面的三个没有任何运气。还有什么我做错了吗?