在 Jdbc 中,当我查看表中的记录时,我得到 varible some value 1 和 variable some value wo 而不是值本身

发布于 2024-11-08 12:37:18 字数 1748 浏览 0 评论 0原文

我已经建立了java数据库连接,这是我的代码。我的问题是,当我从数据库查看记录时,即select * from recordtbl;然后我没有得到这些变量中必须存在的值< code>Variable Filename =somevalue1;

variable checksumno =somevalue two;

代码是 -

package md5IntegrityCheck;
import java.sql.*;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;


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


      Connection con=null;
      PreparedStatement pst=null;

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




          // get the file name here and store it in filename variable;


          String filename="somevalueone ";
          String chksumno="somevaluetwo";



          System.out.println("Step 2");
          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!");
      } 
      catch(Exception e)
        {
         System.out.println("ERROR : "+e);
        }


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

I have make java data base connectivity and here is my code.My problem is that when i view the record from database ie select * from recordtbl; then I dont get the values that must be in these variable Varible Filename =somevalue1;

variable checksumno =somevalue two;

The code is -

package md5IntegrityCheck;
import java.sql.*;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;


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


      Connection con=null;
      PreparedStatement pst=null;

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




          // get the file name here and store it in filename variable;


          String filename="somevalueone ";
          String chksumno="somevaluetwo";



          System.out.println("Step 2");
          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!");
      } 
      catch(Exception e)
        {
         System.out.println("ERROR : "+e);
        }


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

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

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

发布评论

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

评论(2

旧竹 2024-11-15 12:37:18

您没有执行 commit() 也没有关闭连接。尽管 JDBC 中的默认设置是 autocommit=true规范非常清楚:

强烈建议
应用程序明确提交或
回滚之前的活动事务
调用 close 方法。如果
close 方法被调用,并且有一个
交易活跃,结果为
实现定义的。

此外,在没有提交的情况下,如果后面的代码打开不同的数据库连接,该连接可能看不到代码中早期的更新。在插入后添加 commit()

You are not doing a commit() and also not closing the connection. Even though the default setting in JDBC is autocommit=true, the spec is pretty clear:

It is strongly recommended that an
application explicitly commits or
rolls back an active transaction prior
to calling the close method. If the
close method is called and there is an
active transaction, the results are
implementation-defined.

Also, without commit, if later code opens a different DB connection, that connection may not see the updates from earlier in the code. Add a commit() after the insert.

两个我 2024-11-15 12:37:18

像这样尝试一下。如果需要,请摆脱所有这些打印语句并使用调试器单步执行代码。

您正在连接到 Oracle 吗?如果是,为什么使用 ODBC 驱动程序而不是 Oracle 类型四、JDBC驱动

package md5IntegrityCheck;

import java.sql.*;


public class MD5IntegrityCheck
{
  public static void main(String[] args)
  {
       Connection con=null;
       PreparedStatement pst=null;

       try
       {
           // Are you trying to connect to Oracle?  Why aren't you using an Oracle JDBC driver?
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
           con = DriverManager.getConnection("jdbc:odbc:recordtbl","scott","tiger");
           String filename="somevalueone ";
           String chksumno="somevaluetwo";
           pst=con.prepareStatement("insert into recordtbl values(?,?)");
           pst.setString(1,filename);
           pst.setString(2,chksumno);
           int numRowsInserted = pst.execute();
           System.out.println("# rows inserted: " + numRowsInserted);
       } 
       catch (Exception e)
       {
          e.printStackTrace();
       }
       finally
       {
           try { if (pst != null) pst.close(); } catch (Exception e) { }
           try { if (con != null) con.close(); } catch (Exception e) { }
       }
    }
}

Try it like this. Get rid of all those print statements and use a debugger to step through the code if you must.

Are you connecting to Oracle? If yes, why are you using an ODBC driver instead of an Oracle Type IV JDBC driver?

package md5IntegrityCheck;

import java.sql.*;


public class MD5IntegrityCheck
{
  public static void main(String[] args)
  {
       Connection con=null;
       PreparedStatement pst=null;

       try
       {
           // Are you trying to connect to Oracle?  Why aren't you using an Oracle JDBC driver?
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
           con = DriverManager.getConnection("jdbc:odbc:recordtbl","scott","tiger");
           String filename="somevalueone ";
           String chksumno="somevaluetwo";
           pst=con.prepareStatement("insert into recordtbl values(?,?)");
           pst.setString(1,filename);
           pst.setString(2,chksumno);
           int numRowsInserted = pst.execute();
           System.out.println("# rows inserted: " + numRowsInserted);
       } 
       catch (Exception e)
       {
          e.printStackTrace();
       }
       finally
       {
           try { if (pst != null) pst.close(); } catch (Exception e) { }
           try { if (con != null) con.close(); } catch (Exception e) { }
       }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文