当前位置:   article > 正文

kotlin mysql数据库_android studio使用JDBC访问mysql数据库(Kotlin方法)

android studio中用kotlin连接mysql数据库

工具:mysql-connector-java-5.1.48.jar,mysql version 5.6.19,android studio内置模拟器

1.connection

importjava.sql.DriverManagerinterfaceConnection {

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")

}

}

}

View Code

注意事项: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));

20200127-ls3SVO.png

<1>

importjava.lang.Exceptionimportjava.sql.Driverimportjava.sql.DriverManagerimportjava.sql.PreparedStatementinterfaceConnection {

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")

}

}

}

20200127-SmHDVe.png

<2>Util类

classDbUtil {

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 0until args.size){

ps.setObject(i+1,args[i])

}

ps.execute()

println("操作数据成功...")

}).start()

}catch(e: Exception) {

e.printStackTrace()

}

}

}

增加查询

var conn:java.sql.Connection?=nullvar 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()

}returnconn

}

fun updateDatabase(sql:String,vararg args:Any) {try{

Thread({

var conn=connection()

var ps= conn!!.prepareStatement(sql)for(i in 0until 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 0until 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 0until columns) {

var value=resultSet.getObject(i+1)

var columnName=rsmd.getColumnName(i+1)

println("${columnName},${value}")

}

}

}).start()

}catch(e:Exception){

e.printStackTrace()

}

}

}

android studio使用JDBC访问mysql数据库(Kotlin方法)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/1009321
推荐阅读
相关标签
  

闽ICP备14008679号