代码:
import java.sql.DriverManager
/**
* @date :2021/3/31 22:28
* @author :xiaotao
* @description :访问mysql中表数据
*/
object DataFromMysql {
def main(args: Array[String]): Unit = {
val conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/wxt?characterEncoding=UTF-8&useSSL=false",
"root",
"123456")
val statement = conn.prepareStatement("select cid,cname,money,dt from t_result")
val set = statement.executeQuery()
while (set.next()) {
//打印到控制台
println(set.getString("cid") + "," + set.getString("cname") + "," + set.getString("money") + "," + set.getString("dt"))
}
set.close()
statement.close()
conn.close()
}
}
控制台:
E:\develop\Java\jdk1.8.0_171\bin\java.exe ...
2,手机,500,2021-03-31
1,家具,800,2021-03-31
3,服装,600,2021-03-31
Process finished with exit code 0