【发布时间】:2015-01-23 11:26:29
【问题描述】:
我正在尝试使用免费服务器连接和应用程序,尝试使用 Web 服务但根本无法做到,所以现在我正在尝试使用 jdbc 来做到这一点:
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
getActionBar().hide();
new Con().execute();}
private class Con extends AsyncTask<Void, Void, Void> {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://31.170.165.253/u802222393_gp";
// Database credentials
static final String USER = "u802222393_books";
static final String PASS = "aboasendar";
@Override
protected Void doInBackground(Void... params) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER).newInstance();
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql= "SELECT id, first, last, age FROM test";
ResultSet rs = stmt.executeQuery(sql);
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
// Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
// STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}// nothing we can do
catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}// end finally try
catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// end try
System.out.println("Goodbye!");
return null;
}
它总是在我搜索了很多的 URL 上给出一个例外,但没有找到关于我应该如何编写 URL 的答案
Logcat :
【问题讨论】:
-
您是否尝试过使用 php 将数据库中的数据导出到 json 数组中,然后使用 JSONObject 在您的设备中读取它们??
-
这个项目是给大学的,他们希望我们使用 jdbc
-
无论您的代码放在哪里,它都无法到达目标地址。我查看了您正在使用的服务,您似乎无法从外部访问 MySQL 数据库。葡萄牙语链接:hostinger.com.br/base-de-conhecimento/5.html
-
当您是免费用户时,在托管程序上忘记它。也许你可以用 localhost 来做,但你必须把笔记本电脑带到大学..我在 netbeans 中创建一个应用程序时遇到了同样的问题,试图从托管服务器的免费用户那里做同样的事情....我不推荐它
-
所以没有免费的,非常感谢您的反馈