1、下载MySQL-connector-jave.jar包
地址如下:https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.46
2、将jar包移到如图所示的位置,然后右键 add as library
3、在AndroidManifest.xml里面添加如下代码:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4、新建一个工具类 DBUtils 用于连接数据库
public class DBUtil { private final static String driver = "com.mysql.jdbc.Driver"; private final static String url = "jdbc:mysql://本机的IP地址:3306/leave_application_system?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; private final static String username = "root"; private final static String password = "你的密码"; 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(); } } }
注意不能使用localhost代替本机的IP地址,因为Android虚拟机连接MySQL数据库 需要 本机的ip地址,否则会报错
5、新建类 Dao 用于对数据进行操作. (以UserDao为例)
public class UserDao { private DBUtil DBUtils; //查询用户的方法 public boolean select(String username, String password, String role) throws Exception { Connection conn = null; Statement state = null; ResultSet rs = null; try { conn = DBUtils.getConnect(); state = conn.createStatement(); String sql = "select * from user where userNo = '" + username + "' and password = '" + password + "' and role ='" + role + "'"; System.out.println(sql); rs = state.executeQuery(sql); if(rs.next()){ return true; }else{ return false; } } catch (Exception e) { e.printStackTrace(); return false; } finally { DBUtils.release(conn, state, rs); } } }
6、在Android 的 Activity类中调用上面的Dao类对象,进行数据操作 (以登录为例)button1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { Thread thread = new Thread() { public void run() { UserDao userDao = new UserDao(); String userNo = text1.getText().toString(); String password = text2.getText().toString(); try { Message msg = new Message(); Bundle bundle = new Bundle();
bundle.putString("result", String.valueOf(userDao.select(userNo, password, ROLE))); msg.setData(bundle);
//将查询得到的结果通过handler异步发送给主线程 handler.sendMessage(msg); } catch (IOException ex) { ex.printStackTrace(); String result = ex.toString(); Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("result", result); msg.setData(bundle); handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); } });
}
private void initHandlers(){
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String result = msg.getData().getString("result");
/.../
};
}
注意调用Dao类的代码(即JDBC连接数据库)不能放在主线程上,不然就会报错。上面使用thread创建线程,在线程中进行数据操作。
问题:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/sql/SQLType;
解决方法:
如果你的jar包版本为8,将jar包的版本降低,我使用的是5.1.46版本
注意此时的配置代码为:
private final static String driver = "com.mysql.jdbc.Driver"; private final static String url = "jdbc:mysql://10.81.98.193:3306/leave_application_system?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
MySQL的版本是8但是我使用5.1.46版本的jar包仍然行得通
在原本出现com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException 错误的时候,我尝试将jar包版本换成8,并按照此处的要求修改配置,但是还是行不通。
标签:jdbc,String,rs,new,static,MySQL,Studio,null,public From: https://www.cnblogs.com/zbw-m/p/17237640.html