Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.sql.{Connection, DriverManager, Statement}
  2.  
  3. object SimpleJDBC {
  4.  
  5. def main(args: Array[String]): Unit = {
  6. val url = "jdbc:mysql://localhost:3306/message_weibo"
  7. val username = "root"
  8. val password = "root"
  9.  
  10. var connection: Connection = null
  11.  
  12. //sql_1内容可变
  13. val sq1_1: String = "INSERT weibo_attitude VALUES(NULL,'2019-09-12 13:24:57',1,0,0);"
  14. val sql_2: String = "SELECT SUM(wbnum_pos) FROM weibo_attitude;"
  15. val sql_3: String = "SELECT SUM(wbnum_neg) FROM weibo_attitude;"
  16. val sql_4: String = "SELECT SUM(wbnum_neu) FROM weibo_attitude;"
  17.  
  18. try {
  19. //make the connection
  20. classOf[com.mysql.jdbc.Driver]
  21.  
  22. connection = DriverManager.getConnection(url, username, password)
  23.  
  24. //create the statement, and run the select query
  25. val statement1 = connection.createStatement()
  26. val statement2 = connection.createStatement()
  27. val statement3 = connection.createStatement()
  28. val statement4 = connection.createStatement()
  29. val resultSet1 = statement1.executeUpdate(sq1_1)
  30.  
  31. val resultSet2 = statement2.executeQuery(sql_2)
  32. val resultSet3 = statement3.executeQuery(sql_3)
  33. val resultSet4 = statement4.executeQuery(sql_4)
  34. while (resultSet2.next()) {
  35. //wbnum_pos存储当前positive态度的item的总数,得到的wbnum_pos是INT类型
  36. val wbnum_pos = resultSet2.getString("SUM(wbnum_pos)")
  37. }
  38. while (resultSet3.next()) {
  39. //wbnum_neg存储当前negative态度的item的总数,得到的wbnum_neg是INT类型
  40. val wbnum_neg = resultSet3.getString("SUM(wbnum_neg)")
  41. }
  42. while (resultSet4.next()) {
  43. //wbnum_neu存储当前neutral态度的item的总数,得到的wbnum_neu是INT类型
  44. val wbnum_neu = resultSet4.getString("SUM(wbnum_neu)")
  45. }
  46.  
  47. } catch {
  48. case e: Exception => e.printStackTrace()
  49. } finally {
  50. //release the resource
  51. if (connection == null) {
  52. connection.close()
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement