助けてくれてありがとう!
私はこの問題を解決することができました。私がしたこと、それが誰かを助けることができるなら、私はここにどのように投稿しますか:
まず、 DataSource を作成する Driver() のために、他の Classe を作成します。
public class Driver implements DataSource {
public static Connection connection = null;
public static final String url = "jdbc:mysql://url/dataBaseName";
protected String user = "userName";
protected String passwd = "password";
//Andoid related, haven't tested the log so, i don't know if this work.
private String state = Environment.getExternalStorageState();
public Driver(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public PrintWriter getLogWriter() throws SQLException {
PrintWriter logWriter = null;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
try {
logWriter = new PrintWriter(state + "/com.me.appName/Logfile.log");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return logWriter;
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setLogWriter(PrintWriter arg0) throws SQLException {
// TODO Auto-generated method stub
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
try {
DriverManager.setLogWriter(new PrintWriter(
state + "/com.me.appName/Logfile.log"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection() throws SQLException {
if (connection != null) {
System.out.println("Cant create Connection");
} else {
connection = DriverManager.getConnection(
url, user, passwd);
}
return connection;
}
@Override
public Connection getConnection(String userName, String password)
throws SQLException {
// TODO Auto-generated method stub
if (connection != null) {
System.out.println("Cant craete a Connection");
} else {
connection = DriverManager.getConnection(
url, userName,
password);
}
return connection;
}
}
最後に私のメイン Classe で:
onCreate() でドライバーをインスタンス化し、必要なすべてのリクエストを遅滞なく行うことができます:)
public void onClick(View v) {
// Perform action on click
new Thread(new Runnable() {
public void run() {
try {
Statement statement;
//The .getConnection is where i handle the DriverManager.
connexion = driver.getConnection();
statement = connexion.createStatement();
ResultSet resultat = statement
.executeQuery("SELECT name FROM users;");
while (resultat.next()) {
resultId = resultat.getString("name");
}
driver.setLoginTimeout(5);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}).start();
それは本当に便利です。
もう一度ありがとう、あなたの答えに投票できなくてごめんなさい、そしていつかあなたを助けることを願っています!