我
"Select * from 人事档案 where 性别 ='男'"
能查出结果
而
String[] param = {"男"};
"Select * from 人事档案 where 性别 =?"
sqlbean.setParam(param);
就不能查出结果,并不报任何错误insert into 人事档案(姓名,性别) values(“暗暗啊”,“男”),插入记录虽然成功却是乱码救人一命!!!!!!!!!!!!!!
"Select * from 人事档案 where 性别 ='男'"
能查出结果
而
String[] param = {"男"};
"Select * from 人事档案 where 性别 =?"
sqlbean.setParam(param);
就不能查出结果,并不报任何错误insert into 人事档案(姓名,性别) values(“暗暗啊”,“男”),插入记录虽然成功却是乱码救人一命!!!!!!!!!!!!!!
Statement state = con.createStatement();
String sql = "insert into 人事档案(姓名,性别) values('暗暗啊','男')";
state.executeUpdate(sql);
sql = "Select * from 人事档案 where 性别 ='男'";
ResultSet rs = state.executeQuery(sql);
可是如果再JAVABEAN理作一个executeQuery执行查询,setStatement设置查询语句,setparam给参数赋植(定义参数数组string[])
再JSP理定义参数数组string[] param,param={"男","作业员"}
先setStatement:"Select * from 人事档案 where 性别 =?and =职务?"
然后setparam(param)
最后executeQuery
不报错,吴结果
JAVABEAN
***********import java.sql.*;
public class QueryBean
{
public String query_statement; /*定义sql语句*/
public String param[]; /*查询条件,或者是新的记录*/
public ResultSet result=null;
public Connection conn;
public void setParam(String[] param)
{
this.param=param;
}
public void setQuerystatement(String query_statement)
{
this.query_statement=query_statement;
}
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
Connection conn1;
Class.forName(driverName);
conn1=DriverManager.getConnection(jdbcURL,username,passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
}
/*获取查询结果*/
public ResultSet getResult()
{
try
{
PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
if (param!=null)
for(int i=0;i<param.length;i++)
select_stm.setString(i+1,param[i]);
result=select_stm.executeQuery();
}
catch(Exception e)
{
System.out.println(e);
}
return result;
}
/*对数据库进行增加记录操作*/
public void insertRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement insert_stm=conn.prepareStatement(query_statement);
if (param!=null)
for(int i=0;i<param.length;i++)
insert_stm.setString(i+1,param[i]);
insert_stm.executeUpdate();
insert_stm.close();
conn.commit();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
} /*对数据记录进行更改操作*/
public void updateRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement update_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int i=0;i<param.length;i++)
update_stm.setString(i+1,param[i]);
update_stm.executeUpdate();
update_stm.close();
conn.commit();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
/*删除数据记录*/
public void deleteRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement delete_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int i=0;i<param.length;i++)
delete_stm.setString(i+1,param[i]);
delete_stm.executeUpdate();
delete_stm.close();
conn.commit();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
public static String toChinese(String strvalue)
{
try
{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}
catch(Exception e)
{
return null;
}
}
} *******************
JSP
******************<%@ page language="java" import="DataBaseBEAN.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<HTML>
<BODY>
<jsp:useBean id="sqlbean" scope="session" class="DataBaseBEAN.QueryBean"/>
<%!
String name,sqlstmt;
ResultSet rs;
%>
<%
String[] param = {"男"};
sqlbean.setConnection("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=wrerp","sa","");
sqlstmt="Select * from 人事档案 where 性别 =?"; sqlbean.setQuerystatement(sqlstmt);
sqlbean.setParam(param);
rs=sqlbean.getResult();
%>
<%
while(rs.next())
{
%>
<br>
name:<%=name%>
<hr>
</br>
<%
name=sqlbean.toChinese(rs.getString(2));
}
%>
<%
rs.close();
%>
</BODY>
</HTML>
String sql = "Select * from 人事档案 where 性别 = ?";
String[] param = {"男"};
PreparedStatement st = con.prepareStatement(sql);
ResultSet rs = null;
if (param!=null){
for(int i=0;i<param.length;i++) st.setString(i+1,param[i]);
rs=st.executeQuery();
}
String str=new String(str.getBytes("ISO8859_1"), "GBK");