日期:2014-05-16 浏览次数:20465 次
drop table test purge;
create table test(
zipcode char(6)
);
insert into test values('12345');
commit;
--以下四种查询都有记录返回。
select * from test;
select * from test where zipcode='12345';
select * from test where zipcode='12345 ';
select * from test where zipcode='12345 ';
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.matt.util.DBUtil;
public class TestJDBC {
@Test
public void testJC() throws SQLException{
// 使用工具类获取数据库连接。
Connection connection = DBUtil.getConnection();
// 这一段代码有疑问:
String sql = "SELECT * FROM test WHERE zipcode=?"; // 表结构请查看test.sql
PreparedStatement ps = connection.prepareStatement(sql);
// ps.setString(1, "12345 "); // 情况一:有记录返回。
/*
情况二:
当使用以下一行代码时:没有记录返回。
但是,在SQL Developer中如下SQL直接查询时,都是有记录返回的:
select * from test where zipcode='12345';
这是为什么?
*/
ps.setString(1,"12345");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
System.out.println(rs.getString("zipcode"));
}else{
System.out.println("没有记录");
}
// 使用工具类关闭数据库连接。
DBUtil.closeConnection();
}
}