scala訪問MySQL數據庫例子:java
import java.sql.{Connection,ResultSet,DriverManager} import scala.util.control.Exception.Catch //import java.sql.DriverManager object DataAnalysisTest { def main(args: Array[String]): Unit = { if(args.length!=3) { println("參數錯誤!") println("使用方法:DataAnalysisTest 數據庫名 用戶名 密碼") return } //驅動名稱 val driver = "com.mysql.jdbc.Driver" //數據庫名 var dbdata = args(0) //用戶名 val username = args(1) //密碼 val password = args(2) val dbc = "jdbc:mysql://localhost:3306/"+dbdata+"?user="+username+"&password="+password //初始化數據鏈接 var connection:Connection =null // try { Class.forName(driver) connection = DriverManager.getConnection(dbc) val statement = connection.createStatement() val rs = statement.executeQuery("select * from imtesttable ") while(rs.next()) { println(rs.getInt("id")) println(rs.getInt("f0")) } connection.close() } catch{ case e:Exception=>e.printStackTrace() } } }
程序在eclipse中執行都很正常。命令行用scala來執行(上面源代碼所在的目錄是C:\Users\joe\eclipse-workspace\HelloScala\):mysql
C:\Users\joe\eclipse-workspace\HelloScala\bin>scala DataAnalysisTest 數據庫名 用戶名 密碼sql
程序也能正常執行。如今想用java命令行來執行:數據庫
先在此將相關的環境變量顯示給你們看:eclipse
C:\Users\joe\eclipse-workspace\HelloScala\bin>echo %SCALA_HOME%
C:\Program Files (x86)\scalaspa
我如今開始在cmd中輸入命令:.net
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"D:/Program Files (x86) /eclipse-jee-oxygen-3a-win32-x86_64/eclipse/plugins/org.scala-lang.scala-library .source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar";D:\software\mysql-connector- java-5.1.46/mysql-connector-java-5.1.46.jar;%SCALA_HOME%\lib\* DataAnalysisTest dbtest root 123abc 錯誤: 找不到或沒法加載主類 Files
而後我將最後一個scala相關的jar庫放在前面:命令行
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"D:/Program Files (x86) /eclipse-jee-oxygen-3a-win32-x86_64/eclipse/plugins/org.scala-lang.scala-library .source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar";"%SCALA_HOME%\lib\*";D:\soft ware\mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.jar DataAnalysisTe st dbtest root 123abc 1 10 2 11
說明成功執行。怎麼會這樣呢?難道xxx\*這樣的jar包含格式不能放在最後面嗎?應該不是這樣的。再作實驗以下:scala
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"D:/Program Files (x86) /eclipse-jee-oxygen-3a-win32-x86_64/eclipse/plugins/org.scala-lang.scala-library .source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar";D:\software\mysql-connector- java-5.1.46/mysql-connector-java-5.1.46.jar;"%SCALA_HOME%\lib\*" DataAnalysisTe st dbtest root 123abc 1 10 2 11
再次說明只是由於沒有加雙引號的緣由。再後來發現第一個jar包是源碼包,根本就不須要:code
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;D:\software\mysql-connector- java-5.1.46/mysql-connector-java-5.1.46.jar;"%SCALA_HOME%\lib\*" DataAnalysisTe st dbtest root 123abc 1 10 2 11
若是將上面的mysql的jar包放在bin目錄下執行以下命令也能夠正確執行:
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;mysql-connector-java-5.1.46.jar;"%SCALA_HOME%\lib\*" DataAnalysisTe st dbtest root 123abc 1 10 2 11
換成下面的命令則不能正確執行:
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"%SCALA_HOME%\li b\*" DataAnalysisTest dbtest root 123abc java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at DataAnalysisTest$.main(DataAnalysisTest.scala:32) at DataAnalysisTest.main(DataAnalysisTest.scala)
換成以下命令則能正確執行:
C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;".\*";"%SCALA_HO ME%\lib\*" DataAnalysisTest dbtest root 123abc 1 10 2 11