工具:mysql-connector-java-5.1.48.jar,mysql version 5.6.19,android studio内置模拟器
1.connection
import java.sql.DriverManager interface Connection { fun init(){ var driver="com.mysql.jdbc.Driver" var url="jdbc:mysql://10.0.2.2:3306/test" var user="root" var psd="rs123456" try { Class.forName(driver).newInstance() println("连接成功1") }catch (e:Exception){ e.printStackTrace() println("连接失败") } try{ Thread({ DriverManager.getConnection(url,user,psd) println("连接成功2") }).start() }catch (e:Exception) { e.printStackTrace() println("连接失败2") } } }
注意事项:android studio内置模拟器ip地址一定要设置为10.0.2.2
连接问题参考https://blog.csdn.net/qq_37141773/article/details/84326163
2.PreparedStatement
【准备工作】
在数据test中建立table
use test;
create table stuinfo(id int,name varchar(20));
<1>
import java.lang.Exception import java.sql.Driver import java.sql.DriverManager import java.sql.PreparedStatement interface Connection { fun init(){ var driver="com.mysql.jdbc.Driver" var url="jdbc:mysql://10.0.2.2:3306/test" var user="root" var psd="rs123456" try { Class.forName(driver).newInstance() println("连接成功1") }catch (e:Exception){ e.printStackTrace() println("连接失败") } try{ Thread({ val conn=DriverManager.getConnection(url,user,psd) var sql:String="insert into stuinfo(id,name)values(?,?)" val ps:PreparedStatement=conn.prepareStatement(sql) ps.setString(1,"3") ps.setString(2,"vocus") ps.execute() }).start() }catch (e:Exception) { e.printStackTrace() println("连接失败2") } } }
<2>Util类
class DbUtil { fun updateDatabase(sql:String,vararg args:Any) { var driver = "com.mysql.jdbc.Driver" var url = "jdbc:mysql://10.0.2.2:3306/test" var user = "root" var psd = "rs123456" //var sql = "insert into stuinfo(id,name) values(?,?)" try { Class.forName(driver).newInstance() println("数据库连接成功...") } catch (e: Exception) { e.printStackTrace() } try { Thread({ var conn = DriverManager.getConnection(url, user, psd) var ps = conn!!.prepareStatement(sql) for(i in 0 until args.size){ ps.setObject(i+1,args[i]) } ps.execute() println("操作数据成功...") }).start() } catch (e: Exception) { e.printStackTrace() } } }
增加查询
var conn:java.sql.Connection?=null var driver = "com.mysql.jdbc.Driver" var url = "jdbc:mysql://10.0.2.2:3306/test" var user = "root" var psd = "rs123456" fun connection():java.sql.Connection?{ //var sql = "insert into stuinfo(id,name) values(?,?)" try { Class.forName(driver).newInstance() println("数据库连接成功...") } catch (e: Exception) { e.printStackTrace() } try { conn = DriverManager.getConnection(url, user, psd) }catch (e:Exception){ e.printStackTrace() } return conn } fun updateDatabase(sql:String,vararg args:Any) { try { Thread({ var conn = connection() var ps = conn!!.prepareStatement(sql) for(i in 0 until args.size){ ps.setObject(i+1,args[i]) } ps.execute() println("操作数据成功...") }).start() } catch (e: Exception) { e.printStackTrace() } } fun queryDatabase(sql:String,vararg args:Any){ try{ Thread({ var conn=connection() var ps=conn!!.prepareStatement(sql) for(i in 0 until args.size){ ps.setObject(i+1,args[i]) } var resultSet=ps.executeQuery() var rsmd=resultSet.getMetaData() var columns=rsmd.columnCount println("执行查询成功...以下为返回结果...") if(resultSet.next()) { for (i in 0 until columns) { var value=resultSet.getObject(i+1) var columnName=rsmd.getColumnName(i+1) println("${columnName},${value}") } } }).start() }catch (e:Exception){ e.printStackTrace() } } }