public BusSub(String str){
Connection con =null;
PreparedStatement ps =null;
ResultSet rs =null;
String sql ="delete from bussiness where content=?";
try{
// 加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 获取连接
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL", "123", "123"); ps =con.prepareStatement(sql);
//str ="算法";
//去掉注释时,调用该函数也无法删除“算法”,而在Oracle中,用sql:delete from bussiness where content ='算法'
//则没问题,正常删除,为什么
ps.setString(1,str); //问题:如果传入的是中文汉字的话,数据删不了,英文就行,为什么呀?
System.out.println(" "+str);
rs=ps.executeQuery();
rs.close();
}
catch(ClassNotFoundException e1){
System.out.println("找不到驱动");
}
catch(SQLException e2){
System.out.println("未连接到服务器");
e2.printStackTrace();
}
finally{
if(con !=null)
try{
con.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
if(ps !=null)
try{
ps.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
if(rs !=null)
try{
rs.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
}
}
问题见注释,朋友说是编码问题,我觉得有可能,大家可有解决方案?
Connection con =null;
PreparedStatement ps =null;
ResultSet rs =null;
String sql ="delete from bussiness where content=?";
try{
// 加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 获取连接
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL", "123", "123"); ps =con.prepareStatement(sql);
//str ="算法";
//去掉注释时,调用该函数也无法删除“算法”,而在Oracle中,用sql:delete from bussiness where content ='算法'
//则没问题,正常删除,为什么
ps.setString(1,str); //问题:如果传入的是中文汉字的话,数据删不了,英文就行,为什么呀?
System.out.println(" "+str);
rs=ps.executeQuery();
rs.close();
}
catch(ClassNotFoundException e1){
System.out.println("找不到驱动");
}
catch(SQLException e2){
System.out.println("未连接到服务器");
e2.printStackTrace();
}
finally{
if(con !=null)
try{
con.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
if(ps !=null)
try{
ps.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
if(rs !=null)
try{
rs.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
}
}
问题见注释,朋友说是编码问题,我觉得有可能,大家可有解决方案?
驱动的bug,换个版本的驱动。
我对oracle驱动真是很无奈,烂
看数据库中是否会有乱码
要有:
说明是服务器与客户端字符集不匹配
一般遇到这个问题,首先会想到先在数据库中查询下面的结果是否为中文字符集,如果不是就改成中文字符集:
select * from V$NLS_PARAMETERS where parameter = 'NLS_CHARACTERSET ';
导致编码问题很多,一一排除一下
另
finally{
if(con !=null)
try{
con.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
if(ps !=null)
try{
ps.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
if(rs !=null)
try{
rs.close();
}
catch(SQLException e4){
e4.printStackTrace();
}
}
顺序不要这样写
我用的是Eclipse,oracle
我将?替换成'中文字符'可以正常删除... String sql ="delete from bussiness where content=’中文字符‘";
还是搞不懂的是我的测试语句System.out.println(" "+str); 已正常打印出了传入的中文
那就是 ps.setString(1,str); 没成功啦!
那为什么str是英文有没问题咧...严重郁闷!
char[] const = "中文字符".toCharArray();
char[] var = str.toCharArray();
然后把这两个char数据的char值一个一个打印出来(转换为int),看值是否相等.
2.如果第一种情况是相等的,那content是char,varchar2?还是nchar,nvarchar2?
3.我想不出其它情况可以造成你这样的结果?
改了如下语句:
String sql ="delete from bussiness where content='"+str+"'";
ps =con.prepareStatement(sql);
//ps.setString(1,str); 注释掉
rs=ps.executeQuery();
删除中英文都没问题了,可是我还是不明白其中原因,为什么我顶楼的代码不能
查中文
暂时不结贴,希望有人能给出原因...
期待中...
后在试
executeQuery();如果是直接操作,JDBC允许executeQuery();直接操作所有SQL语句.
如果是预编译语句,会对传入的参数类型,长度的操作类型做检查,简单说statement就是将sql语句传递给数据库直接执行的.
用executeQuery()还是executeUpdate()只影响对操作结果的返回,但不影响把SQL语句传给数据库.而预编译语句是把要执行的SQL编译成数据库内的函数,所以在调用时要对传入的参数类型,参数长度,和返回类型和长度做检查.executeQuery()不能接收delete操作的函数返回值.所以不能正确执行.