| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 923 人关注过本帖
标题:[求助]连接数据库出错,什么原因呢?
只看楼主 加入收藏
飞辉
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-8-9
收藏
 问题点数:0 回复次数:7 
[求助]连接数据库出错,什么原因呢?
这是连接数据库的bean:

package com.bean;
import java.sql.*;
public class conndb{
private Connection con;
private ResultSet rset;
public static Connection getConnection() throws SQLException{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:odbc:LocalServer");
}
public ResultSet executeQuery(String sql){
try
{
con=conndb.getConnection();
Statement statement=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rset=statement.executeQuery(sql);
}
catch(SQLException e)
{
e.printStackTrace();
}
return rset;
}
public int executeUpdate(String sql){
int count=0;
Statement statement=null;
try
{
con=conndb.getConnection();
statement=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
count=statement.executeUpdate(sql);
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(statement!=null)
statement.close();
if(con!=null)
con.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
return count;
}
public void freeRset(ResultSet rset){
try
{
if(rset!=null)
{
rset.close();
con.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


这是报错信息:

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]用户 '(null)' 登录失败。原因: 未与信任 SQL Server 连接相关联。
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at com.bean.conndb.getConnection(conndb.java:16)
at com.bean.conndb.executeQuery(conndb.java:21)
at org.apache.jsp.do_005flogin_jsp._jspService(do_005flogin_jsp.java:66)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
2007-8-12 10:32:09 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.do_005flogin_jsp._jspService(do_005flogin_jsp.java:67)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)

哪位朋友帮我一下,感激不尽!
搜索更多相关主题的帖子: 数据库 public bean odbc 
2007-08-12 10:43
枫飘天涯
Rank: 2
来 自:北京
等 级:新手上路
威 望:4
帖 子:251
专家分:0
注 册:2006-10-13
收藏
得分:0 
用户名为空了吧  看看你的用户名是什么   

*、還⒋聼媽媽德話吧 晩點再戀愛吧
2007-08-12 11:09
飞辉
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-8-9
收藏
得分:0 

不懂得改哪里啊!下面是验证页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="databean" scope="session" class="com.bean.conndb"/>
<%
ResultSet rset=null;
String username=request.getParameter("userName");
String password=request.getParameter("password");
if(session.getAttribute("islogin")==null)
{
try
{
String sql="select * from user where userName='"+username+"' and password='"+password+"'";
rset=databean.executeQuery(sql);
if(rset.next())
{

//out.print("<script language=\"JavaScript\">alert(\"登陆成功\");window.close();</script>");
%>
<script language="javascript">
opener.location.reload();
window.close();
</script>
<%
session.setAttribute("islogin","true");
}
else
{
out.print("<script language=\"JavaScript\">alert(\"用户名称或密码不正确,请重新登陆\");window.close();</script>");
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
else
{
out.print("<script language=\"JavaScript\">alert(\"您已经登陆了\");window.close();</script>");
}
%>


2007-08-12 11:45
飞辉
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-8-9
收藏
得分:0 

这个是前台页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head><title>登陆窗口</title></head>
<body bgcolor="#449933">
<form action="do_login.jsp">
<table>
<tr>
<td>用户名称</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>用户密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登 陆">&nbsp &nbsp<input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>

总共就这三个页面,数据源也已经连接正常了,可是怎么还出错,不知道哪里的原因!

2007-08-12 11:47
无缘今生
Rank: 2
等 级:新手上路
威 望:3
帖 子:523
专家分:7
注 册:2007-6-25
收藏
得分:0 
错误信息都已经告诉你了,在连接数据库的时候你需要提供用户名和密码。
我也遇到过这个问题,加上就正常了。

时不再来!!!
2007-08-12 12:44
ermaogogo
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-8-5
收藏
得分:0 
return DriverManager.getConnection("jdbc:odbc:LocalServer","","");
2007-08-12 13:06
ermaogogo
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-8-5
收藏
得分:0 

N久没写了,可以参考一下.
/*
* システム名 : Training
* ファイル名 : DBTool.java
* 作成者(会社名) : liuchuang
* 作成年月日 : 2006/08/01
* 改訂履歴 : V1.0L10 2007/08/01 liuchuang(Hewlett-Packard) 初版作成
* Copyright : Copyright (c) 2007 Hewlett-Packard Development Company,Ltd.
*/
package cq.training.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;
import sun.jdbc.odbc.*;
/**
*
* this class is the get Db connetion and close tool class
*
* @author liuchuang
* @version 1.0 01/08/2006
*/
public class DBTool {
// the DB connection url
private static final String URL = "jdbc:oracle:thin:@XXXXXXXXXXXXXX:XXXXXX";

// the DB User's loginName
private static final String USERNAME = "XXXXX";

// the DB User's login passwordF
private static final String PASSWORD = "XXXXX";

/**
* this mothed is get the DB connection
*
* @return Connection
*/
public static Connection getConnection() {
Connection con = null;
try {
// locad the DriverClass in the memory
Class.forName("oracle.jdbc.driver.OracleDriver");
// get the DB connetion
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
Logger.log(e);
} catch (SQLException ex) {
Logger.log(ex);
}
return con;
}

/**
* this method is close the DB ConnectionF
*
* @param res the resultset instance,if appoint null,will do nothing with
* this instance
* @param stmt the Statement instance,if appoint null,will do nothing with
* this instance
* @param con the connection instance,if appoint null,will do nothing with
* this instance
*/
public static void colse(ResultSet res, Statement stmt, Connection con) {
try {
try {
if (res != null) {
res.close();
}
} catch (Exception e) {
Logger.log(e);
}// end colse the resultSet instance
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
Logger.log(e);
}// end close the statement instance
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
Logger.log(e);
}// end close the connection instance
} catch (Exception e) {
Logger.log(e);
}
}

public static void main(String[] args) {
try {
System.out.println("the db connetion status:"+DBTool.getConnection());
} catch (Exception e) {
e.printStackTrace();
}

}
}

2007-08-12 13:18
evollock
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2007-7-4
收藏
得分:0 

这个
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]用户 '(null)' 登录失败。原因: 未与信任 SQL Server 连接相关联。
你第一句不就抛出异常了吗..说明第一句里有错误
java.lang.NullPointerException
空指针异常..就是说明你的变量有的没有得到植...

综合来看。忘记写用户名密码了吧...
我不习惯你这种连接方式。.

2007-08-12 16:09
快速回复:[求助]连接数据库出错,什么原因呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054127 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved