赞
踩
工具: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));
<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")
}
}
}
<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()
}
}
}
原文:https://www.cnblogs.com/vocus/p/12235312.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。