2011-05-21 22 views
0

*語法錯誤*連接對象的範圍jdbc中的Con

我正面臨着JDBC的問題。 在這一行:

Connection con =DriverManager.getConnection("jdbc:odbc:recordtbl","scott","tiger"); 

這裏在con錯誤出現,我重命名這個變量。

這是我的代碼:

package md5IntegrityCheck; 
import java.io.UnsupportedEncodingException; 
import java.security.NoSuchAlgorithmException; 
import java.util.Scanner; 

import java.sql.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class MD5IntegrityCheck 
{ 

    public static void main(String[] args) 
    throws UnsupportedEncodingException, NoSuchAlgorithmException 
    { 


     Statement stmt; 
     ResultSet rs; 
     Connection con=null; 
     PreparedStatement pst=null; 
     try 
     { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      Connection con =DriverManager.getConnection("jdbc:odbc:recordtbl","scott","tiger"); 
        } 
      catch(Exception ee) 
      {ee.printStackTrace();} 

      System.out.println("Step 1"); 
      System.out.println(con.isClosed()); 

      System.out.println("Enter the filename"); 
      Scanner scanner = new Scanner(System.in); 
      String filename=scanner.nextLine(); 
      String chksumno="somevalue2"; 


      // get the file name here and store it in filename variable; 
      MD5 md5 = new MD5(filename); 
      System.out.println("test"); 
      chksumno = md5.getHashValue(); 

      System.out.println("Step 2"); 
      try{ 
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
       con = DriverManager.getConnection("jdbc:odbc:recordtbl","scott","tiger"); 

       pst=con.prepareStatement("insert into recordtbl values(?,?)"); 

      //System.out.println(pst.isClosed()); 
      System.out.println("Step3"); 
      pst.setString(1,filename); 
      System.out.println("Step 4"); 
      pst.setString(2,chksumno); 
      System.out.println("Step 5"); 
      pst.execute(); 

      System.out.println("Statement was EXECUTED!"); 
      con.close(); 
      } 


      catch(Exception ee) 
      {ee.printStackTrace();} 


    if (args.length <= 0) 
    { 
     Md5Gui gui = new Md5Gui(); 
     gui.runGui(); 
    } 
    else 
    { 
     DoWork runningProgram = new DoWork(); 
     runningProgram.run(args); 
    } 
    } 
} 
+0

請發表你得到 – Mat 2011-05-21 08:28:17

回答

2

你已經宣佈所謂con在您嘗試聲明它第二次範圍內的局部變量。這在Java中是不允許的。只要改變它,以便第二次它是一個分配的,而不是聲明:

con = DriverManager.getConnection("jdbc:odbc:recordtbl", "scott", "tiger"); 

(有各種其他的事情我會改變你的代碼,包括關閉連接等在finally塊,但是這應該解決您的眼前的問題。)

+0

哎喬恩請chages在代碼中的錯誤提示.. – user762357 2011-05-21 08:41:17

+0

哎JON我的代碼在編譯步驟一個..rest所有的未編譯... PLZ更正它 – user762357 2011-05-21 08:43:11

+0

@ user762357:恐怕我沒有時間去檢查每一件可能出錯的小東西。你是否密切關注編譯器的消息,並試圖自己解決出了什麼問題?除了其他任何東西,你還沒有發佈MD5,Md5Gui和DoWork類,所以我*不能*糾正一切。 – 2011-05-21 09:06:09