I. BACKGROUND
Because of the project needs, the MySQL database needs to be connected to the Sbt+scala project. As a result of the previous use of Maven+java to rely on management, in the Sbt+scala aspect is also constantly groping, hereby recorded, as a small module accumulation of knowledge.
Second, the system environment
The versions of Scala, SBT, and IDE are as follows
Scala version ==> 2.11.8
SBT version ==> 0.13.8
Idea IntelliJ version ==> 2016.2.2
Third, step
3.1 New SBT Project
3.2 Adding student classes and program portals
The project structure is as shown
Where the student code is as follows
Package Com.hust.grid.leesf.domain /** * Created by LEESF on 2016/8/28. */ class Student (Val id:int, Val name:string, Val age:int) { = "id =" + id + ", name =" + Name + ", age =" +
Age}
The Mysqldemo code is as follows
Packagecom.hust.grid.leesf.entranceImportjava.sql. {Connection, DriverManager, SQLException}Importcom.hust.grid.leesf.domain.Student/*** Created by LEESF on 2016/8/28. */Object Mysqldemo {def main (args:array[string]): Unit={val URL= "Jdbc:mysql://localhost:3306/test?user=root&useunicode=true&characterencoding=utf-8"var conn:connection=NULL Try { //load MySQL driverClassof[com.mysql.jdbc.driver] Conn=drivermanager.getconnection (URL) val stmt=conn.createstatement val SQL= "SELECT * FROM Student"Val rs=stmt.executequery (SQL) while(Rs.next) {println (NewStudent (Rs.getint (1), rs.getstring (2), Rs.getint (3))) } } Catch { CaseE:sqlexception =E.printstacktrace CaseE:exception =E.printstacktrace}finally { if(Conn! =NULL) {conn.close} }}}
The BUILD.SBT code is as follows
Name: = "Mysqldemo"version:= "1.0"scalaversion:= "2.11.8"+ = "MySQL"% " Mysql-connector-java "%" 5.1.6 "
3.3 New Student Table
Select Test Database
Execute SQL script as follows
create table ' student ' (' ID ' int (11 ) not null auto_increment, ' name ' varchar (20 ) default null , ' age ' tinyint (2 ) default null , primary key
Insert record script as follows
Insert into Values - ); Insert into Values );
3.4 Execution procedures
Executive Mysqldemo
The results are as follows
id = 1, name = LEESF, age =2, name = Dyd, age = 24
Note: The above results indicate that the database has been successfully accessed and the data is obtained.
Iv. Summary
The above recorded the use of Sbt+scala access to MySQL data process, the entire demo has been uploaded to GitHub, the need for the park friends can directly fork, thank you for the view of the park friends.
"Knowledge Accumulation" Sbt+scala+mysql demo