jsp连接数据库问题,各位帮帮忙!

standout 2002-04-02 11:30:24
用SQL SERVER2000做数据库支持。用JDBC-ODBC桥 实现与数据库连接,建立了一数据源名为leaveword.dsn,
代码如下 :
String url=new String("jdbc:odbc:leaveword");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(SQL);

SQL="select * from leaveword";
ResultSet rs1=stmt.executeQuery(SQL);
while(rs1.next())
{
out.print(rs1.getString("id"));
out.print(rs1.getString("name"));
out.print(rs1.getString("ip"));
out.print(rs1.getString("address"));
out.print("<br>");
}
在提交过程提示:
exception

javax.servlet.ServletException: [Microsoft][ODBC 驱动程序 管理器] 未发现数据源名称并且未指定默认驱动程序

请问问题出在哪里,如何解决啊!急。谢了!

...全文
5 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueair 2002-04-02
Connection con=DriverManager.getConnection(url,"UserName","Passwd");

建立ODBC数据源时最好用系统数据源。
回复
chenpu2000 2002-04-02
建立ODBC数据源时最好用系统数据源。
回复
chenpu2000 2002-04-02
另外: Connection con=DriverManager.getConnection(url);
对ACCESS数据库可以,要是SQL SERVER要加上:
Connection con=DriverManager.getConnection(url,"UserName","Passwd");

其中:UserName 是:用户名。 Passwd是:密码。

看你报的错误还不是这个问题。



回复
chenpu2000 2002-04-02
你的程序帮你测试过了,没问题,可是
String url=new String("jdbc:odbc:leaveword");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(SQL);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(1)
SQL="select * from leaveword";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(2)
ResultSet rs1=stmt.executeQuery(SQL);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(3)
这是怎么回事,没在(1)之前写SQL语句就用?

连接数据库语句没问题,好好看看你的SQL SERVER 和 ODBC的连接吧!
回复
zhipop 2002-04-02
http://www.csdn.net/expert/topic/616/616322.xml?temp=.475857
回复
dy1978 2002-04-02
Connection con=DriverManager.getConnection(url);
改为Connection con=DriverManager.getConnection(url,"sa","");
试试
回复
standout 2002-04-02
发现数据字中字段值给小了,改正后出现如何错误:但查看表后,发现数据已经插入了,
先看错误:
javax.servlet.ServletException: No ResultSet was produced
代码如下:
<%@ page import="java.sql.*" %>
<%
int n=0;
String cid=new String();String cname=new String();
String cword=new String();
String SQL=new String();
String cip=new String();

String url=new String("jdbc:odbc:leaveword");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url,"sa","");

cid=request.getParameter("txtid");
cname=request.getParameter("txtname");
cword=request.getParameter("txtcontent");
cip=request.getRemoteAddr();
n=cid.length()*cname.length()*cword.length();
if(n==0)
{
out.println("留言中有为空项目,请返回!");
}
else
{
SQL="insert into leaveword_t values('"+cid+"','"+cword+"','"+cip+"','"+cname+"')";

Statement stmt=con.createStatement();
stmt.executeQuery(SQL);
Statement stmt1=con.createStatement();
SQL="select * from leaveword_t";
ResultSet rs1=stmt1.executeQuery(SQL);
while(rs1.next())
{
out.print(rs1.getString("id"));
out.print(rs1.getString("name"));
out.print(rs1.getString("ip"));
out.print(rs1.getString("address"));
out.print("<br>");
}

}
%>
我感觉是ResultSet rs1=stmt1.executeQuery(SQL);
这段出了错误 ,为什么啊??
回复
standout 2002-04-02
这是改完后的所有代码,
<%@ page import="java.sql.*" %>
<%
int n=0;
String cid=new String();String cname=new String();
String cword=new String();
String SQL=new String();
String cip=new String();

String url=new String("jdbc:odbc:leaveword");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url,"sa","");

cid=request.getParameter("txtid");
cname=request.getParameter("txtname");
cword=request.getParameter("txtcontent");
cip=request.getRemoteAddr();
n=cid.length()*cname.length()*cword.length();
if(n==0)
{
out.println("留言中有为空项目,请返回!");
}
else
{
SQL="insert into leaveword_t values('"+cid+"','"+cword+"','"+cip+"','"+cip+"')";

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(SQL);

SQL="select * from leaveword";
ResultSet rs1=stmt.executeQuery(SQL);
while(rs1.next())
{
out.print(rs1.getString("id"));
out.print(rs1.getString("name"));
out.print(rs1.getString("ip"));
out.print(rs1.getString("address"));
out.print("<br>");
}

}
%>

改完后,好像连接成功了,但提示出:
javax.servlet.ServletException: [Microsoft][ODBC SQL Server Driver][SQL Server]将截断字符串或二进制数据。
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告