JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 JDBC連結Mysql問題
SecretD





發文: 15
積分: 0
於 2018-08-20 01:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mysqltest;
 
import java.sql.*;
 
public class mysqltest {
 
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        System.out.println("Loading driver...");
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded!");
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot find the driver in the classpath!", e);
    }
    String url = "jdbc:mysql://localhost:3306/Daniel";
    String username = "root";     //範例,使用者ID
    String password = "sssddd248";  //範例,使用者密碼
    Connection con = null;
    Statement stat = null;   //執行,傳入之sql為完整字串 
    ResultSet rs = null;   //結果集 
    PreparedStatement pst = null; 
    try {
        System.out.println("Connecting database...");
        con = DriverManager.getConnection(url, username, password);
        System.out.println("Database connected!");
    } catch (SQLException e) {
        throw new RuntimeException("Cannot connect the database!", e);
    } finally {
        System.out.println("Closing the connection.");
        if (con != null) try { con.close(); } catch (SQLException ignore) {}
    }
    try { 
      stat = con.createStatement(); 
      stat.executeUpdate("CREATE TABLE User (id  INTEGER, name VARCHAR(20), passwd  VARCHAR(20))"); 
      //這個字串就是傳給mySQL server的指令,這裡是建立一個名叫User的Table
    } 
    catch(SQLException e) { 
      System.out.println("CreateDB Exception :" + e.toString()); 
    }
    
  
    try { 
      pst = con.prepareStatement("insert into User(id,name,passwd) select ifNULL(max(id),0)+1,?,? FROM User"); 
      //這裡的問號是等下要傳送的參數
      pst.setString(1, "Daniel");   //傳送第一個參數(取代第一個問號)
      pst.setString(2, "sssddd248");   //傳送第二個參數(取代第二個問號)
      pst.executeUpdate(); 
    } 
    catch(SQLException e) { 
      System.out.println("InsertDB Exception :" + e.toString()); 
    } 
    try { 
      stat = con.createStatement(); 
      rs = stat.executeQuery("select * from User "); 
      System.out.println("ID\t\tName\t\tPASSWORD"); 
      while(rs.next()) { 
          System.out.println(rs.getInt("id")+"\t\t"+ 
              rs.getString("name")+"\t\t"+rs.getString("passwd")); 
      } 
    } 
    catch(SQLException e) { 
      System.out.println("DropDB Exception :" + e.toString()); 
    } 
    try { 
      if(rs!=null) { 
          rs.close(); 
          rs = null; 
      } 
      if(stat!=null) { 
          stat.close(); 
          stat = null; 
      } 
      if(pst!=null) { 
          pst.close(); 
          pst = null; 
      } 
    } 
    catch(SQLException e) { 
      System.out.println("Close Exception :" + e.toString()); 
    }
  }
 
  
}

此程式碼是透過網頁上的範例
執行後的問題
1
2
3
4
5
6
7
8
9
10
11
Loading driver...
Exception in thread "main" java.lang.RuntimeException: Cannot find the driver in the classpath!
  at mysqltest.mysqltest.main(mysqltest.java:14)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  at java.net.URLClassLoader.findClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Unknown Source)
  at mysqltest.mysqltest.main(mysqltest.java:11)

請各位幫忙解答,謝謝


reply to postreply to post
作者 Re:JDBC連結Mysql問題 [Re:SecretD]
roytsang





發文: 618
積分: 1
於 2018-08-20 05:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Error message 很清楚地說出你的JDBC driver 不在class path 內,
你有download mysql jdbc driver 嗎?
如果有請將mysql-connector-java-X.x.x-bin.jar 放進你的class path 內,問題應該會解決的。


reply to postreply to post
作者 Re:JDBC連結Mysql問題 [Re:roytsang]
SecretD





發文: 15
積分: 0
於 2018-08-20 05:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我下載了,也看了網站上的Buildpath方式,可是放好後,問題依然沒解決耶,還是一樣的錯誤
JAVA環境 JRE 1.8
DRIVER : mssql-jdbc-7.0.0.jre8


reply to postreply to post
作者 Re:JDBC連結Mysql問題 [Re:SecretD]
roytsang





發文: 618
積分: 1
於 2018-08-20 05:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你這個是MS-SQL 的JDBC driver,不是MYSQL jdbc driver.

reply to postreply to post
作者 Re:JDBC連結Mysql問題 [Re:roytsang]
SecretD





發文: 15
積分: 0
於 2018-08-20 06:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
了解,已經解決,謝謝幫忙

reply to postreply to post
» JWorld@TW »  JDBC/SQL討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8