In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use ScalikeJDBC to operate MySQL database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Environment:
IDEA+Maven+ScalikeJDBC+MySQL5.6
1 New Scala project for Maven
2 add dependencies to pom.xml files:
2.11.8
2.5.2
5.6.23
Org.scalikejdbc
Scalikejdbc_2.11
${scalikejdbc.version}
Org.scalikejdbc
Scalikejdbc-config_2.11
${scalikejdbc.version}
Mysql
Mysql-connector-java
${mysql.version}
Ch.qos.logback
Logback-classic
1.2.3
3 add Scala object
Package com.ruozedata
Import scalikejdbc._
Object ScalaLikeJdbc {
Def main (args: Array [String]): Unit = {
/ / load driver
Class.forName ("com.mysql.jdbc.Driver")
/ / specify database connection url,userName,password
Val url = "jdbc:mysql://192.168.1.108:3306/ruozedata?useUnicode=true&characterEncoding=UTF8"
Val userName = "root"
Val password = "root"
/ / set database connection parameters
Val settings = ConnectionPoolSettings (
InitialSize = 5
MaxSize = 20
ConnectionTimeoutMillis = 3000L
ValidationQuery = "select 1 from test") / / test table is a specially created test table
/ / build database connection pool
ConnectionPool.singleton (url, userName, password, settings)
/ / New data
Val insertResult: Int = DB autoCommit {implicit session = >
SQL ("insert into user (id, name,age) values"). Bind (1, "Zhangsan", 15). Update (). Apply ()
SQL ("insert into user (id, name,age) values"). Bind (2, "Ruoze", 16). Update (). Apply ()
SQL ("insert into user (id, name,age) values"). Bind (3, "Jepson", 17). Update (). Apply ()
}
Println (insertResult) / / prints the value of the insertResult variable. If the addition is successful, it returns 1, otherwise 0.
/ / define the User class
Case class User (id: Int, name: String, age: Int)
/ / query data operation
Val allColumns = (rs: WrappedResultSet) = > User (
Id = rs.int ("id")
Name = rs.string ("name")
Age = rs.int ("age"))
Val users: List [User] = DB readOnly {implicit session = >
SQL ("select * from user") .map (allColumns) .list.apply ()
}
/ / Loop through the users object and output
For (user
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.