【Scala】基於8.0版本的jdbc進行資料庫連接出現
阿新 • • 發佈:2018-11-01
下面這個程式碼可以正常執行,沒有問題
import java.sql.{Connection, DriverManager, ResultSet, Statement} object Main { def main(args: Array[String]): Unit = { val driver = "com.mysql.cj.jdbc.Driver" val url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai" val user = "root" val password = "404notfounD" Class.forName(driver) val conn: Connection = DriverManager.getConnection(url, user, password) val SQL = "select * from tran" val stmt: Statement = conn.createStatement() val rs: ResultSet = stmt.executeQuery(SQL) while (rs.next()) { println(rs.getString("statis_date")) } rs.close() stmt.close() conn.close() } }
但如果匯入包的時候,吧
import java.sql.{Connection, DriverManager, ResultSet, Statement}
替換為
import java.sql._
上面的程式碼就會報錯
因為java.sql._下也有一個Array類,與scala.array會衝突,main的引數就會變成了java.sql.Array型別,因此最好就是用哪個匯入哪個