我的开发环境是Eclipse3.4.1 Web服务器是Apache Tomcat/5.0.28,我安装了jdbc,应用程序的方式访问数据库没错,程序如下:import java.sql.*;public class AccessDataBase {

public static final String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
public static final String user="sa";
public static final String password="";

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String queryString="SELECT title,price From Titles";

Connection conn=null;
Statement st=null;
ResultSet rs=null;

try
{
Class.forName(drivername);
conn=DriverManager.getConnection(url, user, password);
st=conn.createStatement();
rs=st.executeQuery(queryString);

while(rs.next())
{
System.out.println("书名:"+rs.getString("title")+"定价:"+rs.getDouble("price"));
}
rs.close();
st.close();
conn.close();
}
catch(Throwable t)
{
t.printStackTrace(System.out);
}
finally
{
try
{
if(rs!=null)
rs.close();
}
catch(Exception e)
{}

try
{
if(st!=null)
st.close();
}
catch(Exception e)
{}

try
{
if(conn!=null)
conn.close();
}
catch(Exception e)
{}
}
}}
这没有问题,可是用这种方式挪到Web页中,这是我根据书上的例子改的,如下:<%@ page import="java.sql.*" %>
<%-- 定义常用函数 --%>
<%!
// 得到一个数据库的连接
Connection getConnection() throws SQLException
{
// 定义驱动程序
String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
// 定义连接字符串
String CONNSTR = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
try
{
Class.forName( DBDRIVER );
}
catch ( ClassNotFoundException ex )
{
ex.printStackTrace( System.err );
}
return DriverManager.getConnection( CONNSTR,"sa","" );
}// 判断一个字符串是否为空
boolean isEmptyString( String str )
{
return str==null || str.length()==0 || str.trim().length()==0;
}
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>WEB数据库维护系统</title>
<style>
TD,INPUT,SELECT {FONT-SIZE: 12px}
</style>
<script language="javascript">
function checkvalue()
{
if( document.forms["main"].sql.value.length==0 )
{
alert( "请输入要执行的SQL语句!" );
document.forms["main"].sql.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
WEB数据库维护系统
<p><!-- 显示输入框,供用户输入需要执行的SQL语句 -->
<table width="80%" border="0">
<tr>
<td>请输入您要执行的SQL语句:</td>
</tr>
<tr>
<form name="main" method="post" action="execsql.jsp" onsubmit="return checkvalue();">
<td>
<textarea name=sql cols=80 rows=15></textarea>
</td>
<td valign="bottom">
<input type="submit" value="执行" />
</td>
</form>
</tr>
</table><hr><%-- 接收用户的输入,显示查询的结果 --%>
<%
// 获得用户输入的SQL语句
String sql = request.getParameter( "sql" );
if( !isEmptyString( sql) )
{
out.println( "您所执行的SQL语句为:" + sql + "<br>" );
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try
{
conn = getConnection();
st  = conn.createStatement();
if( st.execute( sql ) )
{
// 执行结果为ResultSet
rs = st.getResultSet(); // 得到ResultSet的描述信息
ResultSetMetaData rsmd = rs.getMetaData();
// // 得到ResultSet的列数目
int nColumnCount = rsmd.getColumnCount();
out.println( "您的查询结果为:<br>" );
out.println( "<table width=\"90%\" border=\"1\">" );
out.println( "<tr>" );
for( int i=1; i<=nColumnCount; i++ )
{
out.println( "<td><b>" + rsmd.getColumnName( i ) + "</b></td>" );
}
out.println( "</tr>" ); while( rs.next() )
{
out.println( "<tr>" );
for( int i=1; i<=nColumnCount; i++ )
{
out.println( "<td>" + rs.getString( i ) + "</td>" );
}
out.println( "</tr>" );
}
}
else
{
// 执行结果为正数
out.println( "更新记录的数目为:" + st.getUpdateCount() );
}
}
catch( SQLException ex )
{
out.println( ex );
}
finally
{
// 关闭,释放资源
try
{
if( rs!=null ) rs.close();
}
catch( Exception ex ) {} try
{
if( st!=null ) st.close();
}
catch( Exception ex ) {}
try
{
if( conn!=null ) conn.close();
}
catch( Exception ex ) {}
}
}
%>
</center>
</body>
</html>可是在Web页中总是提示异常,java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unsupported VM encoding MS936
另外,显示出来汉字全是乱码,如果加上<%@ page contentType="text/html;charset=gb2312"%>这句,又会有org.apache.jasper.JasperException: Unsupported encoding: gb2312 的异常,所以我就不加了,不知道高手们遇到过这个问题没有,那个访问数据的问题,我根据网上查的资料,jdk从1.4.2换到1.5,jdbc 也换成sp3,sql也打上了sp3,问题还是没有解决,现在弄的我都学不下去了,没想到学习Java还这么困难,希望高手们,给与指点,高分求

解决方案 »

  1.   


      你在网上找一下看看
    MS936是汉字的编码codepage。   
      你的驱动不支持汉字字符
        换个版本·~用1.4的一般稳定~
    Unsupported encoding: gb2312 不支持gb2312这种编码格式
    你有没有在jsp的开头写下 
    <%@ page contentType="text/html;charset=gb2312"%>
      

  2.   

    我已经换了几个jdbc了,能不能提供一个支持MS936的,我还有点怀疑不是jdbc的原因,因此通过JAVA aplication用jdbc就没事,而建立tomcat 项目就不行,我怀疑是哪个设置没安好,是不是与tomcat有关,jdk也换到1.5了,就差没换tomcat了,我在jsp里加上<%@ page contentType="text/html;charset=gb2312"%>,访问页面的时候,编译就通不过,出现org.apache.jasper.JasperException: Unsupported encoding: gb2312
    的错误,真搞不懂,为什么出现这种情况,别人就没碰到过,我搞了一两天了,快疯了,大家看看还有没有办法
      

  3.   

    没办法了,只能装了Java Studio Enterprise 8,用它配了下环境,然后Eclipse里面指向他安装的那些东西,然后把它自动生成的页面复制过去试一下,竟然OK了,看来还是环境的事