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

Posted vocus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android studio使用JDBC访问mysql数据库(Kotlin方法)相关的知识,希望对你有一定的参考价值。

工具: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")
        }

    }
}
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>

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()
        }

    }
}

 

以上是关于android studio使用JDBC访问mysql数据库(Kotlin方法)的主要内容,如果未能解决你的问题,请参考以下文章

React Native,Android Studio,JDBC,MySql - 用户'root'@'ipaddress'的访问被拒绝

React Native,Android Studio,JDBC,MySql - 拒绝访问用户'root'@'ipaddress'

Android Studio使用JDBC远程连接mysql的注意事项(附示例)

从 Android Studio 访问 MSSQL

在 android studio 中使用 JDBC 登录级别

Android Studio:net.ucanaccess.jdbc.UcanaccessSQLException:错误文件不存在