java web项目中将数据库从SQL Server转为Access出现的常见错误
2015-07-25 23:53
435 查看
1.access数据库里中文显示正常,但是页面上显示的中文字符是乱码。
原因:
没有设置默认的access数据库连接的编码方式
解决方案:
如果之前连接SQL Server数据库是遵照jdbc标准的话,应该会有如下代码
<span style="font-size:14px;">private Connection createConnection() throws SQLException{
if(SQL_account == "" || SQL_account == null || SQL_account.isEmpty()){
Connection realConn =DriverManager.getConnection(url);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}else{
Connection realConn =DriverManager.getConnection(url,SQL账户名,SQL密码);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}
}</span>
将如上代码改为
<span style="font-size:14px;">private Connection createConnection() throws SQLException{
if(SQL_account == "" || SQL_account == null || SQL_account.isEmpty()){
Properties prop = new Properties();
prop.put("charSet", "gbk"); //解决中文乱码
Connection realConn =DriverManager.getConnection(url,prop);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}else{
Properties prop = new Properties();
prop.put("charSet", "gbk"); //解决中文乱码
prop.put( "user", SQL_account );
prop.put( "password", SQL_key );
Connection realConn =DriverManager.getConnection(url,prop);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}
}</span>
2.row_number() over()是SQL Server的函数,然而Access中不支持row_number() over()
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓此段来自百度↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
ACCESS的分页之道,Access中实现row_number() over()聚合函数功能
oracle的分页查询可以利用rowid伪列。
db2的分页查询可以利用row_number() over()聚合函数。
mysql有limit。 access仿佛先天缺陷,仅提供了top n。
那如何利用top来实现分页查询呢?
假设在access中有表t1
随机插入20条数据。
如果以每页5条来显示数据,如果要显示11至15条如何显示?
利用top n功能,前11条可以用以下sql完成。
同样前15条也可以这样:
想要得到11条到15条,估计一般都会想到差集,但access没提供差集except,可以利用not in实现。
如果是一个比较大的表,用not in不能利用索引,使和效率极其低下,又该如何呢?
可以利用左连接来解决问题
这种sql的好处是显而易见的,他有效的利用了表的主键索引。
当然,由于access不能这样判断b.tc1 is null,所以要改用iif(b.tc1,'0','1')='1' 来曲线救国
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑此段来自百度↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
我的修改如下:
连接SQL Server时使用的一段语句
<span style="font-size:14px;">sql="select * from (select ROW_NUMBER() over(order by pubTime DESC) rownumber,* from Zcfg where visible=0) temp where temp.rownumber<=20";</span>
对应的修改为连接Access数据库时的语句为
sql="select top 20 * from (select * from dbo_Zcfg order by pubTime DESC ) where visible=0";
3.从Access中读取中文字符数据时常出现乱码
可以利用如下方法解决
(注:rs 为 ResultSet 对象,news是一个model)
修改前:
<span style="font-size:14px;">news.setTitle(rs.getString("title"))</span>
修改后:
<span style="font-size:14px;">news.setTitle(new String(rs.getBytes("title"),"gbk"));</span>
原因:
没有设置默认的access数据库连接的编码方式
解决方案:
如果之前连接SQL Server数据库是遵照jdbc标准的话,应该会有如下代码
<span style="font-size:14px;">private Connection createConnection() throws SQLException{
if(SQL_account == "" || SQL_account == null || SQL_account.isEmpty()){
Connection realConn =DriverManager.getConnection(url);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}else{
Connection realConn =DriverManager.getConnection(url,SQL账户名,SQL密码);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}
}</span>
将如上代码改为
<span style="font-size:14px;">private Connection createConnection() throws SQLException{
if(SQL_account == "" || SQL_account == null || SQL_account.isEmpty()){
Properties prop = new Properties();
prop.put("charSet", "gbk"); //解决中文乱码
Connection realConn =DriverManager.getConnection(url,prop);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}else{
Properties prop = new Properties();
prop.put("charSet", "gbk"); //解决中文乱码
prop.put( "user", SQL_account );
prop.put( "password", SQL_key );
Connection realConn =DriverManager.getConnection(url,prop);
MyConnection myConnection =new MyConnection(realConn,this);
return myConnection;
}
}</span>
2.row_number() over()是SQL Server的函数,然而Access中不支持row_number() over()
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓此段来自百度↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
ACCESS的分页之道,Access中实现row_number() over()聚合函数功能
oracle的分页查询可以利用rowid伪列。
db2的分页查询可以利用row_number() over()聚合函数。
mysql有limit。 access仿佛先天缺陷,仅提供了top n。
那如何利用top来实现分页查询呢?
假设在access中有表t1
如果以每页5条来显示数据,如果要显示11至15条如何显示?
利用top n功能,前11条可以用以下sql完成。
想要得到11条到15条,估计一般都会想到差集,但access没提供差集except,可以利用not in实现。
可以利用左连接来解决问题
当然,由于access不能这样判断b.tc1 is null,所以要改用iif(b.tc1,'0','1')='1' 来曲线救国
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑此段来自百度↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
我的修改如下:
连接SQL Server时使用的一段语句
<span style="font-size:14px;">sql="select * from (select ROW_NUMBER() over(order by pubTime DESC) rownumber,* from Zcfg where visible=0) temp where temp.rownumber<=20";</span>
对应的修改为连接Access数据库时的语句为
sql="select top 20 * from (select * from dbo_Zcfg order by pubTime DESC ) where visible=0";
3.从Access中读取中文字符数据时常出现乱码
可以利用如下方法解决
(注:rs 为 ResultSet 对象,news是一个model)
修改前:
<span style="font-size:14px;">news.setTitle(rs.getString("title"))</span>
修改后:
<span style="font-size:14px;">news.setTitle(new String(rs.getBytes("title"),"gbk"));</span>
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- SQL中的三值逻辑
- SQL Server 作业批量停止
- 结束SQL阻塞的进程
- 动态生成SQL Server视图作业
- SQL Server 语句操纵数据库
- SQL(结构化查询语句)
- oracle sql日期比较
- linux快速部署mysql服务器
- Access 2000 数据库 80 万记录通用快速分页类
- sql 存储过程分页
- 在WINXP系统上安装SQL Server企业版的方法
- 通过批处理调用SQL的方法(osql)
- SQL Server 存储过程的分页
- ASP程序与SQL存储过程结合使用详解
- SQL SERVER编写存储过程小工具
- 防御SQL注入攻击时需要注意的一个问题
- PostgreSQL教程(十九):SQL语言函数