我的开发环境是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还这么困难,希望高手们,给与指点,高分求
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还这么困难,希望高手们,给与指点,高分求
解决方案 »
- 像这种带方框的评论模块怎么实现?专门的JQuery?
- java
- 与数据库有关 下拉框联动的问题
- tomcat+mysql数据库连接报错
- 如何将html转换成word
- 急!web程序能用javax.swing.JFileChooser来获得客户端设置的保存路径吗?
- 才开始搞spring+strurs,问几个问题,希望能得到答案
- 为什么报oracle Management sever 中某项服务尚未完全初始化啊?为什么启动电脑后有三个oracle进程啊?我怎样解决这二个问题啊?谢谢!
- 请问tomcat4.0以上支持Vector变量吗
- java Spring MVC 定时获取数据库数据,不用刷新更新页面数据
- java urlwriter中的正则表达式组的问题
- 页面显示问题
你在网上找一下看看
MS936是汉字的编码codepage。
你的驱动不支持汉字字符
换个版本·~用1.4的一般稳定~
Unsupported encoding: gb2312 不支持gb2312这种编码格式
你有没有在jsp的开头写下
<%@ page contentType="text/html;charset=gb2312"%>
的错误,真搞不懂,为什么出现这种情况,别人就没碰到过,我搞了一两天了,快疯了,大家看看还有没有办法