API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.sql.DriverManager
Class.forName("com.mysql.jdbc.Driver")
val connect = DriverManager.getConnection("jdbc:mysql://${url:port}/${databsename}[?参数键值对1&参数键值对2..]","root","password")
val prepareStatement = connect.prepareStatement("sql 带问好号的语句") val result = prepareStatement.apply{ setXXX(1,xxx) }.executeQuery()
while (result.next()){ println("${result.getString(1)}") }
result.close() prepareStatement.close() connect.close()
|
连接远程报错问题
如果连接远端 Mysql报 - Host ‘xxx.xx.x.x‘ is not allowed to connect to this MySQL server
,这是数据库操作权限问题,可以通过
1 2 3 4
| mysql -u root -proot mysql> use mysql; mysql> update user set host = '%' where user = 'root'; mysql> flush privileges;
|