【发布时间】:2020-01-28 15:43:02
【问题描述】:
package com.example.test
import android.os.Bundlemportandroid.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import java.sql.DriverManager
import com.mysql.jdbc.Driver
import java.sql.PreparedStatement
import java.sql.Connection
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnDB.setOnClickListener {
DBConn.DBString()
}
setSupportActionBar(toolbar)
fab.setOnClickListener {
view - >
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when(item.itemId) {
R.id.action_settings - > true
else - > super.onOptionsItemSelected(item)
}
}
fun DBCon() {
try {
val url = "jdbc:mysql://localhost:3306/Boxing"
var conn: Connection = DriverManager.getConnection(url, "root", "Guru9")
//conn.close();
if (conn != null) {
println("Connected successfully to Database")
}
} catch (e: Exception) {
println(" Not connected to Database")
}
}
}
【问题讨论】:
-
Workbench 是一个程序,你不能连接到它,但是连接到一个 mysql 服务器