| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 845 人关注过本帖
标题:高手帮帮忙!
只看楼主 加入收藏
yq6780
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-10-17
收藏
 问题点数:0 回复次数:13 
高手帮帮忙!
学习struts阶段,数据库总是报错,在学校就没事,

错误信息:
at java.sql.DriverManager.getConnection(DriverManager.java:512)

at java.sql.DriverManager.getConnection(DriverManager.java:171)

at db.DBHelper.getConnection(DBHelper.java:18)

at dao.DAOHelperUser.login(DAOHelperUser.java:19)

at action.UserAction.login(UserAction.java:26)
我的代码:
package db;

import java.sql.*;

public class DBHelper {
private static Connection conn = null;
private static String url = null;
private static String serverName = null;
private static String portNumber = null;
private static String databaseName = null;
private static String uid = null;
private static String pwd = null;
public DBHelper() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
private static String getConnectionUrl()
{
url = "jdbc:microsoft:sqlserver://";
serverName = "127.0.0.1";
portNumber = "1433";
databaseName = "test";
uid = "sa";
pwd = "";

return url + serverName + ":" + portNumber + ";databaseName=" + databaseName + ";";
}

public static Connection getConnection()
{
try {
conn = DriverManager.getConnection(getConnectionUrl(),uid,pwd);
} catch (SQLException ex) {
ex.printStackTrace();
}
if ( conn != null )
{
System.out.println("连接成功");
}
return conn;
}
}
总报错,在学校就没事。高手们帮看看阿。。
搜索更多相关主题的帖子: 数据库 信息 private package public 
2007-07-10 12:51
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 
我靠
我真不知道该怎么形容的,形形色色的数据库连接!
我看你的连接字符串是最有性格的了,哈哈!
我看你按照前辈我这样的写发就行了,一般人我不告诉他!!

乱78遭的东西我就不写了,代码你自己看看吧!!!

private static Connection conn = null;
static
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(Exception e)
{

}
}

public static Connection getConnection()
{
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=数据库名";
String user = "sa";
String pwd = "";
try{
conn = DriverManager.getConnection(url,user,pwd);
}catch(Exception e){}
return conn;
}

public static void close()
{
if(conn!=null)
{
try
{
conn.close();
}catch(Exception e){}
}
}



你写的那些东西,理论上是没什么问题,但是你不觉得可读性太差了吗?
url = "jdbc:microsoft:sqlserver://";
serverName = "127.0.0.1";
portNumber = "1433";
databaseName = "test";
uid = "sa";
pwd = "";
这些属性到是用了不少!根本没这必要啊!!
最后还来个返回字符串的连接
return url + serverName + ":" + portNumber + ";databaseName=" + databaseName + ";";

别这样写,有些变量少就少,实在少不鸟就没办法了,你每定义一个String类型的变量就得在你内存的堆中或者是常量池中创建一个类的对象实例,要资源开销的....好的变成习惯很重要哦!!!

[此贴子已经被作者于2007-7-10 16:33:57编辑过]


当神已无能为力,就让爷来普度众生吧!
2007-07-10 15:42
yq6780
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-10-17
收藏
得分:0 
谢谢,谢谢。
可是还是不行的。。。
代码应该没问题啊,像你那样写,我也写了N次了。。。
在学校的机器上就能跑。
2007-07-10 15:53
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 
那异常是什么呀
是不是没打SP4的补丁问题啊

当神已无能为力,就让爷来普度众生吧!
2007-07-10 16:04
yq6780
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-10-17
收藏
得分:0 
都打过的,用桥连连的上。。。
2007-07-10 16:06
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 

代码异常都贴出来看看


当神已无能为力,就让爷来普度众生吧!
2007-07-10 16:09
yq6780
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-10-17
收藏
得分:0 
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)

at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)

at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)

at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)

at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)

at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)

at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)

at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)

at java.sql.DriverManager.getConnection(DriverManager.java:512)

at java.sql.DriverManager.getConnection(DriverManager.java:171)

at db.DB.getConnection(DB.java:17)

at dao.DAO.login(DAO.java:14)

at action.loginAction.login(loginAction.java:24)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)

at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)

at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)

at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

at java.lang.Thread.run(Thread.java:534)

这样的。

2007-07-10 16:15
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 
我刚刚的代码中掉了数据库名字不好意思
你在用我的方法进行连接试试看,静太方法的调用用类名你知道吧
其他我就不多说了

当神已无能为力,就让爷来普度众生吧!
2007-07-10 16:18
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 

搞顶了你就跟爷们说声.........


当神已无能为力,就让爷来普度众生吧!
2007-07-10 16:30
yq6780
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-10-17
收藏
得分:0 

不行,而且调试,连接那是没报异常,可是是NULL,用桥连的上。。。


exception

javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
dao.DAO.login(DAO.java:16)
action.loginAction.login(loginAction.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:324)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
2007-07-10 16:40
快速回复:高手帮帮忙!
数据加载中...
 
   



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

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