本人新手,找了好多资源,测试直连的问题,这些代码也是本网站找的,自己稍微改了一下,为何死活连不上去,conn返回都是空的,大神帮我看看。
public class MainActivity extends Activity {
private static final String URL = "jdbc:mysql://www.luhef.cn:3306/xh";
private static final String USER = "*******";
private static final String PASSWORD = "*****";
private Connection conn;
private Button onConn;
private Button onInsert;
private Button onDelete;
private Button onUpdate;
private Button onQuery;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
String str = (String) msg.obj;
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
};
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onConn = (Button) findViewById(R.id.onConn);
onInsert = (Button) findViewById(R.id.onInsert);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
conn = null;
} finally {
conn = null;
}
}
}
public void onConn(View view) {
new Thread(new Runnable() {
@Override
public void run() {
conn = Util.openConnection(URL, USER, PASSWORD);
Message msg = new Message();
msg.obj = conn;
handler.sendMessage(msg);
}
}).start();
}
public void onInsert(View view) {
new Thread(new Runnable() {
@Override
public void run() {
String sql = "insert into users values(15, 'xiaoming')";
Util.execSQL(conn, sql);
Log.i("onInsert", "onInsert");
}
}).start();
}
}
package com.lyz.learning.mysqlandandroid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Util {
final static String DRIVER_NAME = "com.mysql.jdbc.Driver";
public static Connection openConnection(String url, String user,
String password) {
Connection conn = null;
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
conn = null;
} catch (SQLException e) {
conn = null;
}
System.out.println(conn);
return conn;
}
public static void query(Connection conn, String sql) {
if (conn == null) {
return;
}
Statement statement = null;
ResultSet result = null;
try {
statement = conn.createStatement();
result = statement.executeQuery(sql);
System.out.println(result);
if (result != null && result.first()) {
int idColumnIndex = result.findColumn("id");
int nameColumnIndex = result.findColumn("name");
System.out.println("id\t\t" + "name");
while (!result.isAfterLast()) {
System.out.print(result.getString(idColumnIndex) + "\t\t");
System.out.println(result.getString(nameColumnIndex));
result.next();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (result != null) {
result.close();
result = null;
}
if (statement != null) {
statement.close();
statement = null;
}} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
public static boolean execSQL(Connection conn, String sql) {
boolean execResult = false;
if (conn == null) {
return execResult;
}
Statement statement = null;
try {
statement = conn.createStatement();
if (statement != null) {
execResult = statement.execute(sql);
}
} catch (SQLException e) {
execResult = false;
}
return execResult;
}
}