pom依赖:
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.45</version></dependency>
程序:
import java.sql.DriverManager object JdbcMysql {def jdbcUtil( url:String, user:String,password:String ,sql:String) :List[String] = {classOf[com.mysql.jdbc.Driver]val connetcion = DriverManager.getConnection(url,user,password)val stmt =connetcion.createStatement()val rs = stmt.executeQuery(sql)while (rs.next()){val meta = rs.getMetaDataval count = meta.getColumnCountval id = rs.getLong(1)val name = rs.getString(2)val title = rs.getString(3)}rs.close()stmt.close()connetcion.close()return null}def main(args: Array[String]): Unit = {val url = "jdbc:mysql:///xhnn"//这是我的账户密码(连接自己的库就行,别没事连接别人的库)val user = "xhnn"val password = "xhnn"val sql = "select id,name,title from admin_landing_page"jdbcUtil(url,user,password,sql)}}