2013-02-20 95 views
0

異常在線程 「主要」 java.lang.Error的:未解決的問題,編譯: 類型不匹配:不能從java.sql.Statement中轉換爲com.mysql.jdbc.StatementMysql數據庫異常

i am a beginner in java i am trying to use mysql database i have downloaded mysql-connector-java-5.1.23-bin.jar file from mysql.com and i have added this jar file to in my build path of my project but there is an error in the following code Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from java.sql.Statement to com.mysql.jdbc.Statement


package com.example.demo; 

import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.Statement; 

public class DBConnect 
{ 
private static final String userName = "root"; 
private static final String userpwd = "sverma"; 
private static final String CONN_STR = "jdbc:mysql://localhost:3306/phpweb_db"; 

public static void main(String[] args) throws SQLException 
{ 

    Connection conn = null; 
    Statement st = null; 
    ResultSet rs = null; 

    try 
    { 
     DriverManager.getConnection(CONN_STR, userName, userpwd); 
     st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
     rs = st.executeQuery("select * from user"); 
     rs.last(); 
     System.out.println("No of rows: " + rs.getRow()); 

     // System.out.println("Connected Successfully..."); 
    } 
    catch (SQLException e) 
    { 
     System.err.println(e); 

    } 
    finally 
    { 
     if (rs != null) 
     { 
      rs.close(); 
     } 
     if (st != null) 
     { 
      st.close(); 
     } 
     if (conn != null) 
     { 
      conn.close(); 
     } 
    } 
} 

}


+0

歡迎SO!你引用了你的問題。 – 75inchpianist 2013-02-20 19:44:27

+0

使用eclipse?你可以刪除你的導入語句並點擊'ctrl + shift + O'來自動導入所需的類。 – jlordo 2013-02-20 19:48:02

回答

4

錯誤類

import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.Statement; 

應該

import java.sql.Connection; 
import java.sql.Statement; 

事實上,Java的解耦一切從一個特定的數據庫引擎。永遠不需要導入MySQL(或ProgressSQL或...)類。

要具有可這些類在運行時,在try後的第一件事,得到連接之前將是:

Class.forName("com.mysql.jdbc.Driver"); 

該技術將允許讀取從配置文件中的所有字符串,並寫入數據庫獨立的代碼。


缺少:康恩= ...

conn = DriverManager.getConnection(CONN_STR, userName, userpwd); 
+0

謝謝你的回覆...我已經糾正了上面的代碼,因爲你已經指示上面,但我得到的錯誤,而運行時間是**異常線程「主」java.lang.NullPointerException ** – user2092656 2013-02-22 04:49:38

+0

擴展答案。 – 2013-02-22 08:14:05

+0

謝謝先生,我明白了...... – user2092656 2013-02-24 17:19:15

0
package com.example.demo; 

import java.sql.*; 


public class DBConnect 
{ 
private static final String userName = "root"; 
private static final String userpwd = "sverma"; 
private static final String CONN_STR = "jdbc:mysql://localhost:3306/phpweb_db"; 

public static void main(String[] args) throws SQLException 
{ 

    Connection conn; 
    Statement st; 
    ResultSet rs; 

    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     DriverManager.getConnection(CONN_STR, userName, userpwd); 
     st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
     rs = st.executeQuery("select * from user"); 
     rs.last(); 
     System.out.println("No of rows: " + rs.getRow()); 

     // System.out.println("Connected Successfully..."); 
    } 
    catch (SQLException e) 
    { 
     System.err.println(e); 

    } 
    finally 
    { 
     if (rs != null) 
     { 
      rs.close(); 
     } 
     if (st != null) 
     { 
      st.close(); 
     } 
     if (conn != null) 
     { 
      conn.close(); 
     } 
    } 
} 
+0

僅僅回答代碼只是不鼓勵,因爲他們不爲未來的讀者提供很多信息,請提供一些解釋你寫的內容 – WhatsThePoint 2017-12-29 09:46:48