我用jdbc访问Access数据库的时候,碰上个奇怪的现象 "SELECT BidID FROM Bids WHERE UserID = " + userID + " AND RegisterIP = '" + registerIP + "' AND RegisterTime = ?";查出来的值是唯一的吗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加断点取数的目的是想知道哪里出问题了。如果查出来了,查出来的值就是是唯一的。我用的是自己写的连接池。代码如下: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()); } }} 难道没有人知道为什么吗???我用statement是不能把日期/时间类型写入SQL语句的,只能用PreparedStatement变量。其实,如果纪录是唯一的话,查询的时候,我把日期/时间去掉,只用UserID + RegisterIP来查找,也是同样的现象。我就不明白了,插入语句完成以后难道必须等上一定的时间,才能查找那条插入的数据吗????faint~~~~~~~~~~~~~~~~~~ 不妨在if(rs.next()){ 语句前面加上rs.beforeFirst();试试看呢? 我现在查出来了,插入和查找需要同一个connection,现在没有问题了。 java 中如何得到动态网页的内容 进行资源释放问题 请教前辈:我象改变字符串名称的值,具体如下: 如何获得远程主机的系统时间啊 java application程序如何访问tomcat数据库连接池,在控制台中输出数据库表的结果 如何将一个textArea里面的内容保存到一个文件中 Swing类的一个问题 if you need i can help you to pass ocp,ccnp,scjp,ciw..... 如何防止按浏览器右上角的"x" 来关闭浏览器 Java大神安排一下 关于java to exe 欢迎讨论!!! 谁有JBuilder 7盘里面的PDF文档
查出来的值就是是唯一的。我用的是自己写的连接池。
代码如下: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());
}
}
}
只能用PreparedStatement变量。其实,如果纪录是唯一的话,
查询的时候,我把日期/时间去掉,只用UserID + RegisterIP来查找,
也是同样的现象。我就不明白了,
插入语句完成以后难道必须等上一定的时间,
才能查找那条插入的数据吗????faint~~~~~~~~~~~~~~~~~~
插入和查找需要同一个connection,
现在没有问题了。