"SELECT BidID FROM Bids WHERE UserID = " + userID + " AND RegisterIP = '" + registerIP + "' AND RegisterTime = ?";
查出来的值是唯一的吗

解决方案 »

  1.   

    加断点取数的目的是想知道哪里出问题了。如果查出来了,
    查出来的值就是是唯一的。我用的是自己写的连接池。
    代码如下:package mybeans;import java.io.*;
    import java.sql.*;
    import java.util.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */
    class AccessDatabase
    {  //define the member
      ConnectionPool connectionPool = null;
      Connection connection = null;
      Statement statement = null;  //define the functions
      public AccessDatabase()
      {
      }   public void openConnection() throws Exception
       {
        //get the instance of the connectionPool
        connectionPool = ConnectionPool.getInstance();
        //initialize the connections
        connectionPool.initializeConnections();
        //get a connection
        connection = connectionPool.getConnection();
        //create a statement
        statement = connection.createStatement();
       }   public PreparedStatement openConnection(String query) throws Exception
       {
        //get the instance of the connectionPool
        connectionPool = ConnectionPool.getInstance();
        //initialize the connections
        connectionPool.initializeConnections();
        //get a connection
        connection = connectionPool.getConnection();
        //create a prepared statement
        PreparedStatement pstmt = connection.prepareStatement(query);    return pstmt;
       }   public void closeConnection() throws Exception
       {
        statement.close();
        statement = null;    connectionPool.putConnection(connection);
        connection = null;
       }   public void closeConnection(PreparedStatement pstmt) throws Exception
       {
        pstmt.close();
        pstmt = null;    connectionPool.putConnection(connection);
        connection = null;
       }   //This method executes a statement and returns no result set
       //such as INSERT | UPDATE | DELETE etc.
       public void executeSqlWithoutResultSet(String sql) throws Exception
       {
        statement.executeUpdate(sql);
       }   public void executeSqlWithoutResultSet(PreparedStatement pstmt) throws Exception
       {
        pstmt.executeUpdate();
       }   //This method executes a statement and returns a result set
       //such as SELECT
       public ResultSet executeSqlWithResultSet(String sql) throws Exception
       {
        ResultSet rs = null;
        rs = statement.executeQuery(sql);
        return rs ;
       }   public ResultSet executeSqlWithResultSet(PreparedStatement pstmt) throws Exception
       {
        ResultSet rs = null;
        rs = pstmt.executeQuery();
        return rs ;
       }
    }
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */class ConnectionPool
    {
      //define the count of the connections int the pool
      static final int MAX_CONNECTIONS = 10;  //define the members
      static Vector connections = null;
      String URL = "jdbc:odbc:business";
      static ConnectionPool instance = null;  //define the functions
      public ConnectionPool()
      {
      }  public ConnectionPool(String url)
      {
        URL = url;
      }  public static synchronized ConnectionPool getInstance()
      {
        if(instance == null)
          instance  = new ConnectionPool();    return instance;
      }  public synchronized void initializeConnections()
      {
        if(connections == null)
        {
          try
          {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");        connections = new Vector();        int count = 0;
            while (count < MAX_CONNECTIONS)
            {
              Connection connection = DriverManager.getConnection(URL,"","");
              connections.addElement(connection);
              count++;
            }
          }
          catch(Exception e)
          {
           System.out.println(e.toString());
          }
        }
      }  public synchronized Connection getConnection()
      {
        Connection connection = null;
        if(connections == null)
          return null;    if(connections.size() > 0)
        {
          connection = (Connection)connections.elementAt(0);
          connections.removeElementAt(0);
        }    return connection;
      }  public synchronized void putConnection(Connection connection)
      {
        connections.addElement(connection);
        notifyAll();
      }  public synchronized void removeAllConnections()
      {
        try
        {
          if(connections == null)
            return;      int size = connections.size();
          for(int i=0; i<size; i++)
          {
            Connection connection = (Connection)connections.elementAt(i);
            connection.close();
          }      connections.removeAllElements();
          connections = null;
        }
        catch(SQLException e)
        {
          System.out.println(e.toString());
        }
      }
    }
      

  2.   

    难道没有人知道为什么吗???我用statement是不能把日期/时间类型写入SQL语句的,
    只能用PreparedStatement变量。其实,如果纪录是唯一的话,
    查询的时候,我把日期/时间去掉,只用UserID + RegisterIP来查找,
    也是同样的现象。我就不明白了,
    插入语句完成以后难道必须等上一定的时间,
    才能查找那条插入的数据吗????faint~~~~~~~~~~~~~~~~~~
      

  3.   

    不妨在if(rs.next()){ 语句前面加上rs.beforeFirst();试试看呢?
      

  4.   

    我现在查出来了,
    插入和查找需要同一个connection,
    现在没有问题了。