web端的思想在前面已经写了,Android端的思想
跟web端差不多少,其中不一样的就是连接MySQL
数据库,其要求的jar包是5版本左右的,而且还涉及
到线程的问题,在连接MySQL的时候要另外启动一个
线程进行连接,主要思想上没啥改变,就是每次每种
查询要加一个button并且对应Onclick事件,在这个事件
里面启动一个新的线程。
我的队友是江新兵,我俩在此次开发中收获了很多。
package mysqll;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBOpenHelper {
private final static String driver = "com.mysql.jdbc.Driver";
private final static String url = "jdbc:mysql://10.99.121.251/kebiao?useUnicode=true&characterEncoding=UTF-8&useSSL=false";//
private final static String username = "root";
private final static String password = "123456";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动错误");
}
}
//2. 获取连接
public static Connection getConnect() throws Exception {
return DriverManager.getConnection(url, username, password);
}
//3. 释放连接资源
public static void release(Connection conn, Statement st, ResultSet rs) throws Exception {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
}
/*private static String diver = "com.mysql.jdbc.Driver";
//加入utf-8是为了后面往表中输入中文,表中不会出现乱码的情况
private static String url = "jdbc:mysql://10.99.117.52we?characterEncoding=utf-8";
private static String user = "root";//用户名
private static String password = "hzl02096";//密码
*//*
* 连接数据库
* *//*
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(diver);
conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}*/
}
public int findstationsatrole(String roles) throws Exception {
Connection conn = null;
Statement state = null;
ResultSet find = null;
int stationsnumber = 0;
try {
conn = DBOpenHelper.getConnect();
state = conn.createStatement();
roles = roles + "号线";
String sql = "SELECT sname FROM biejing_subway"
+ " WHERE sline='" + roles + "';";
find = state.executeQuery(sql);
stationsnumber = -1;
while (find.next()) {
stationsnumber++;
onrolestation[stationsnumber] = new String();
onrolestation[stationsnumber] = find.getString(1);
}
if (stationsnumber != -1) {
for (int i = 0; i <= (stationsnumber / 2) + 1; i++) {
stationss = stationss + onrolestation[i]+" , ";
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBOpenHelper.release(conn, state, find);
}
return stationsnumber;
}
public int findrolesatstation(String stations) throws Exception {
Connection conn = null;
Statement state = null;
ResultSet find = null;
int stationsnumber = 0;
int rolesnumber = 0;
try {
conn = DBOpenHelper.getConnect();
state = conn.createStatement();
String sql = "SELECT sline FROM biejing_subway"
+ " WHERE sname='" + stations + "';";
find = state.executeQuery(sql);
rolesnumber = -1;
int zhuanhuan = 0;
while (find.next()) {
if (zhuanhuan % 2 == 0) {
rolesnumber++;
onstationatroles[rolesnumber] = new String();
onstationatroles[rolesnumber] = find.getString(1);
}
System.out.print(rolesnumber);
zhuanhuan++;
}
if(rolesnumber!=-1) {
for (int i = 0; i <= rolesnumber; i++) {
rolessss = rolessss + onstationatroles[i] + " , ";
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBOpenHelper.release(conn, state, find);
}
return rolesnumber;
}