当前位置: 技术问答>java相关
使用javabean的一些小问题
来源: 互联网 发布时间:2015-10-06
本文导语: 各位大虾: 我做的关于数据库操作的javabean: package test; import java.sql.*; import java.io.*; import java.lang.*; public class DbSql { String sDBDriver = "COM.ibm.db2.jdbc.net.DB2Driver"; String sConnStr = "jdbc:db2://202.117.106.64:50...
各位大虾:
我做的关于数据库操作的javabean:
package test;
import java.sql.*;
import java.io.*;
import java.lang.*;
public class DbSql
{
String sDBDriver = "COM.ibm.db2.jdbc.net.DB2Driver";
String sConnStr = "jdbc:db2://202.117.106.64:5000/xuebao";
String sUser = "db2inst1";
String sPassword = "db2inst1";
Connection conn = null;
ResultSet rs = null;
public DbSql()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("DbSql(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("DbSql.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
}
catch(SQLException ex)
{
System.err.println("DbSql.executeUpdate:"+ex.getMessage());
}
}
public void executeDelete(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DbSql.executeUpdate:"+ex.getMessage());
}
}
public void executeUpdate(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DbSql.executeUpdate:"+ex.getMessage());
}
}
}
在jsp中使用的时候用于一般的关于英文的查询,更新操作都是成功的,所谓关于英文的意思是传递的变量值是英文符号,但是只要中间有中文符号就不对了,比如说牵扯到中文用户名的查询之类的,系统就不会执行数据库操作。这到底是什么原因阿?
我的数据库是db2,我在主级上用sql语句可以插入查询中文,是怎么回事啊?
很急!!!!!
我做的关于数据库操作的javabean:
package test;
import java.sql.*;
import java.io.*;
import java.lang.*;
public class DbSql
{
String sDBDriver = "COM.ibm.db2.jdbc.net.DB2Driver";
String sConnStr = "jdbc:db2://202.117.106.64:5000/xuebao";
String sUser = "db2inst1";
String sPassword = "db2inst1";
Connection conn = null;
ResultSet rs = null;
public DbSql()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("DbSql(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("DbSql.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
}
catch(SQLException ex)
{
System.err.println("DbSql.executeUpdate:"+ex.getMessage());
}
}
public void executeDelete(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DbSql.executeUpdate:"+ex.getMessage());
}
}
public void executeUpdate(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr,sUser,sPassword);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DbSql.executeUpdate:"+ex.getMessage());
}
}
}
在jsp中使用的时候用于一般的关于英文的查询,更新操作都是成功的,所谓关于英文的意思是传递的变量值是英文符号,但是只要中间有中文符号就不对了,比如说牵扯到中文用户名的查询之类的,系统就不会执行数据库操作。这到底是什么原因阿?
我的数据库是db2,我在主级上用sql语句可以插入查询中文,是怎么回事啊?
很急!!!!!
|
倒,看来你要多看看书了,: adijava(阿迪) 说的是String,他的方法是正确的,你的问题其实是涉及了java的编码问题,它内部用的是unicode编码,你把它传到bean里,因为导致文字的编码不一样咯
你程序中不是有这一句吗?“rs = stmt.executeQuery(sql);”,在传到bean后,你试试把此sql的字符串转码,如:
String temp = new String(sql.getBytes("gb2312"),"8859_1");
然后再继续你的 rs = stmt.executeQuery(temp);
你程序中不是有这一句吗?“rs = stmt.executeQuery(sql);”,在传到bean后,你试试把此sql的字符串转码,如:
String temp = new String(sql.getBytes("gb2312"),"8859_1");
然后再继续你的 rs = stmt.executeQuery(temp);
|
应该是编码不统一造成的,,在你执行sql语句之前,要对sql语句进行转码,
str sql = "select ……";
sql = new String(str.getBytes("gb2312"),"8859_1");
//然后执行sql语句看看,应该没有问题了.
str sql = "select ……";
sql = new String(str.getBytes("gb2312"),"8859_1");
//然后执行sql语句看看,应该没有问题了.