jdbc中连接对象Con的范围

发布于 2024-11-09 04:50:19 字数 2362 浏览 0 评论 0原文

*语法错误 *

我面临 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);
    }
  }
}

*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);
          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);
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您已经在第二次尝试声明的作用域中声明了一个名为 con 的局部变量。这在 Java 中是不允许的。只需更改它,以便第二次它是赋值而不是声明:(

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

我会更改您的代码的其他各种内容,包括 finally 块中的关闭连接等,但这应该可以解决您的问题眼前的问题。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文