开发者

scope of the connection object Con in jdbc

开发者 https://www.devze.com 2023-03-07 22:43 出处:网络
*SyntaxError * I am facing the problem with my JDBC. In this line: Connection con =DriverManager.getConnection(\"jdbc:odbc:recordtbl\",\"scott\",\"tiger\");

*Syntax Error *

I am facing the problem with my JDBC. In this line:

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

here at con error appear that i rename this variable.

This is my code:

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);
          Syste开发者_StackOverflow中文版m.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);
    }
  }
}


You've already declared a local variable called con in the scope where you try to declare it for the second time. That's not allowed in Java. Just change it so that the second time it's an assignment instead of a declaration:

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

(There are various other things I'd change about your code, including closing connections etc in finally blocks, but this should solve your immediate problem.)

0

精彩评论

暂无评论...
验证码 换一张
取 消