我試圖執行下面的代碼,它通過JDBC連接我的Android應用程序到MySQL服務器:錯誤連接空
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
import net.sourceforge.jtds.jdbc.*;
import net.sourceforge.jtds.jdbcx.*;
import java.sql.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "SqlConnection",Toast.LENGTH_SHORT).show();
Connection conn = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:jtds:sqlserver://HOSTNAME/DATABASE_NAME;user=USERNAME;password=PASSWORD;");
Toast.makeText(this, "Connection Opened", Toast.LENGTH_SHORT).show();
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from Tablename;");
if(reset.next() == true){
//Log.w("Data", reset.getString(1));
Toast.makeText(this, reset.getString(1) , Toast.LENGTH_SHORT).show();
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
}
conn.close();
}
catch (Exception e){
//Log.w("Error Connection", ""+e.getMessage());
Toast.makeText(this, "Error Connection" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我已經添加了JTDS 1.2.7在我的項目,並增加了互聯網權限,結果是連接ConnectionNull
*錯誤發生在哪裏? –
我認爲這個網址是錯的 – 2013-11-15 16:03:24
@rocking它的正確格式是什麼?它取決於連接的主機嗎?或者所有的格式都一樣!? – Izzo32