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

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 JDBC驅動偵測不到
allen99999





發文: 3
積分: 0
於 2018-01-24 09:41 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
我是寫sql server的jdbc連接,用MyEclipse 的java模擬器測試時都沒有問題,但我用Java(TM) Platform SE binary 開啟我已經打包好的jar檔時,他老是抓不到驅動源,我的系統是Win 10,JDBC也環境變數已都設好了,我也試過把mssql-jdbc-6.2.2.jre8.jar包直接丟到jre的ext資料夾下,但還是抓不到,以下是我的程式碼:

Connection ct=null;
      PreparedStatement st=null;
      try {
        
        String connectionUrl = "jdbc:sqlserver://127.0.0.1:1433;" +
         "databaseName=weiwei;user=Test;password=********";
        
        
        // 1.加載驅動源(把驅動程序加入內存)
        //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // 2.得到連接[連接到哪個數據源,"用戶名","密碼"]

        
        
       jb1.setBackground(Color.pink);
        ct=DriverManager.getConnection(connectionUrl);
        
        
        
        // 3.創建Statement 或者 PreparedStatement

        // PrepareStatement 主要用來發送SQL語句
        st = ct.prepareStatement("select * from emp where sal>2000 order by sal");
      
        // 4.執行(crud,創建數據庫,備份數據庫,刪除數據庫.......)

        ResultSet b= st.executeQuery();
      while(b.next()){
        int i=b.getInt(1);
        String h=b.getString(2);
        String job=b.getString(3);
        int mgr=b.getInt(4);
        String date=b.getString(5);
        int sal=b.getIntDevil;
        int comm=b.getInt(7);
        int dept=b.getIntMusical Note;
        
        System.out.println(i+" "+h+" "+job+" "+date+" "+sal+" "+comm+" "+dept);
      }
    

      } catch (Exception e) {
        // TODO Auto-generated catch block
        jb1.setText(e.getMessage());
        e.printStackTrace();
      }finally{
        
        try {
          st.close();
    
          if(ct!=null){
            ct.close();
          }
          
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        
      }
    }




allen99999 edited on 2018-01-25 09:07
reply to postreply to post
作者 Re:JDBC驅動偵測不到 [Re:allen99999]
ynchang





發文: 151
積分: 0
於 2018-01-25 09:36 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
因為看不到"環境變數" 究竟是哪一個環境變數,
所以還是先問你, 請問是CLASSPATH嗎?
如果不清楚我在說什麼, 麻煩看下列網址

https://caterpillar.gitbooks.io/javase6tutorial/content/c2_2.html


reply to postreply to post
作者 Re:JDBC驅動偵測不到 [Re:ynchang]
allen99999





發文: 3
積分: 0
於 2018-01-25 16:07 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
感謝您的回覆:
我java的環境變數是正確的,在cmd中也可以執行.jar檔案或javac指令,但碰到有執行jdbc 的sql sevser的.jar時,他程式用javaw 執行或點了都沒反應,我有看sql sevser的官方說明,在ClassPath裡設定了我jdbc的安裝路徑但她還是沒辦法偵測到,但我用測試的java模擬器就可以執行
我的路徑-->C:\Program Files\Microsoft JDBC Driver 6.2 for SQL Server\sqljdbc_6.2\cht\mssql-jdbc-6.2.2.jre8.jar


reply to postreply to post
作者 Re:JDBC驅動偵測不到 [Re:allen99999]
ynchang





發文: 151
積分: 0
於 2018-01-26 03:22 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
// 1.加載驅動源(把驅動程序加入內存)
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


上面這幾行, 在你的code中確定是被註解掉的嗎?


reply to postreply to post
作者 Re:JDBC驅動偵測不到 [Re:ynchang]
allen99999





發文: 3
積分: 0
於 2018-01-26 06:37 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
我兩種都有試過了,我是直接用jdbc的方式,所以如果加上這行的話:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
他會在這行拋出異常:com.microsoft.sqlserver.jdbc.SQLServerDriver
如果沒加的話他會在下一行-->ct=DriverManager.getConnection(connectionUrl);
他會在這行拋出異常:No suitable driver found for jdbc "jdbc:sqlserver://127.0.0.1:1433;databaseName=weiwei;user=Test;password=********";
不過我用的jdbc是mssql-jdbc-6.2.2.jre8.jar 他如果有來源的話應該加不加都沒關係,他會自己試著去載入來源吧Question
我也試過把mssql-jdbc-6.2.2.jre8.jar 直接引到libary中,用Fat jar Exporter打包成一個.jar檔,也不能執行,但在IDE中都可執行,是我的打包方式錯誤了嗎?


reply to postreply to post
作者 Re:JDBC驅動偵測不到 [Re:allen99999]
ynchang





發文: 151
積分: 0
於 2018-01-30 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
https://docs.microsoft.com/en-us/sql/connect/jdbc/using-the-jdbc-driver
看這份文件, 應該是可以不用加Class.forName
不過我建議你先把classpath印出來看看到底有沒有被包含在裡面...


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