当前位置: 代码迷 >> Java Web开发 >> 急!JSP操作SqlServer2000数据库问题
  详细解决方案

急!JSP操作SqlServer2000数据库问题

热度:396   发布时间:2009-12-23 23:54:54.0
急!JSP操作SqlServer2000数据库问题


这个是操作数据库的代码

程序代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {
    private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Blog";
    private final String userName = "sa";
    private final String password = "sa";
    private Connection con = null;
    private Statement stm=null;
   
    /* 通过构造方法加载数据库驱动 */
    public DB(){
        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("加载数据库驱动失败!");
        }        
    }
    /* 创建数据库连接 */
    public void createCon() {
        try {
             con = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取数据库连接失败!");
        }
    }
    /* 获取Statement对象 */
    public void getStm(){
           createCon();
        try {
            stm=con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("创建Statement对象失败!");
        }
    }
    /**
     * @功能 对数据库的增加、修改和删除的操作
     * @参数 sql为要执行的SQL语句
     * @返回值 boolean型值
     */
    public boolean executeUpdate(String sql) {
        System.out.println(sql);
        boolean mark=false;
        try {
            getStm();
            int iCount = stm.executeUpdate(sql);
            if(iCount>0)               
                mark=true;            
            else
                mark=false;
        } catch (Exception e) {
            e.printStackTrace();
            mark=false;
        }
        return mark;
    }
    /* 查询数据库 */
    public ResultSet executeQuery(String sql) {
        ResultSet rs=null;
        try {
            getStm();
            try {
                rs = stm.executeQuery(sql);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("查询数据库失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();         
        }
        return rs;
    }
    /* 关闭数据库的操作 */
    public void closed() {
        if(stm!=null)
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭stm对象失败!");
            }
        if(con!=null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭con对象失败!");
            }
    }
}


这个是控制台信息

程序代码:
2009-12-23 23:34:08 org.apache.catalina.core.AprLifecycleListener init
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Program Files\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin;d:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_7.5.0.zmyeclipse75020090612\tomcat\bin
2009-12-23 23:34:09 org.apache.coyote.http11.Http11Protocol init
严重: Error initializing endpoint
java.net.BindException: Address already in use: JVM_Bind:8080
    at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:501)
    at org.apache.coyote.http11.Http11Protocol.init(Http11Protocol.java:177)
    at org.apache.catalina.connector.Connector.initialize(Connector.java:1059)
    at org.apache.catalina.core.StandardService.initialize(StandardService.java:677)
    at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:792)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:518)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:538)
    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:597)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:260)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:412)
2009-12-23 23:34:09 org.apache.catalina.startup.Catalina load
严重: Catalina.start
LifecycleException:  Protocol handler initialization failed: java.net.BindException: Address already in use: JVM_Bind:8080
    at org.apache.catalina.connector.Connector.initialize(Connector.java:1061)
    at org.apache.catalina.core.StandardService.initialize(StandardService.java:677)
    at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:792)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:518)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:538)
    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:597)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:260)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:412)
2009-12-23 23:34:09 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1180 ms
2009-12-23 23:34:09 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2009-12-23 23:34:09 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.13
2009-12-23 23:34:11 org.apache.coyote.http11.Http11Protocol start
严重: Error starting endpoint
java.net.BindException: Address already in use: JVM_Bind:8080
    at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:501)
    at org.apache.tomcat.util.net.JIoEndpoint.start(JIoEndpoint.java:515)
    at org.apache.coyote.http11.Http11Protocol.start(Http11Protocol.java:204)
    at org.apache.catalina.connector.Connector.start(Connector.java:1132)
    at org.apache.catalina.core.StandardService.start(StandardService.java:531)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    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:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
2009-12-23 23:34:11 org.apache.catalina.startup.Catalina start
严重: Catalina.start:
LifecycleException:  service.getName(): "Catalina";  Protocol handler start failed: java.net.BindException: Address already in use: JVM_Bind:8080
    at org.apache.catalina.connector.Connector.start(Connector.java:1139)
    at org.apache.catalina.core.StandardService.start(StandardService.java:531)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    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:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
2009-12-23 23:34:11 org.apache.catalina.startup.Catalina start
信息: Server startup in 1973 ms
2009-12-23 23:34:11 org.apache.catalina.core.StandardServer await
严重: StandardServer.await: create[8005]:
java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.<init>(ServerSocket.java:185)
    at org.apache.catalina.core.StandardServer.await(StandardServer.java:373)
    at org.apache.catalina.startup.Catalina.await(Catalina.java:630)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:590)
    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:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
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:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.yxq.toolsbean.DB.createCon(DB.java:28)
    at com.yxq.toolsbean.DB.getStm(DB.java:36)
    at com.yxq.toolsbean.DB.executeQuery(DB.java:69)
    at com.yxq.dao.LogonDao.getMaster(LogonDao.java:20)
    at com.yxq.servlet.IndexServlet.<clinit>(IndexServlet.java:68)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:25)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:122)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:918)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:899)
    at java.lang.reflect.Field.set(Field.java:657)
    at org.apache.catalina.loader.WebappClassLoader.clearReferences(WebappClassLoader.java:1615)
    at org.apache.catalina.loader.WebappClassLoader.stop(WebappClassLoader.java:1495)
    at org.apache.catalina.loader.WebappLoader.stop(WebappLoader.java:707)
    at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4534)
    at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:924)
    at org.apache.catalina.startup.HostConfig.undeployApps(HostConfig.java:1180)
    at org.apache.catalina.startup.HostConfig.stop(HostConfig.java:1151)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:313)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1086)
    at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1098)
    at org.apache.catalina.core.StandardEngine.stop(StandardEngine.java:448)
    at org.apache.catalina.core.StandardService.stop(StandardService.java:584)
    at org.apache.catalina.core.StandardServer.stop(StandardServer.java:744)
    at org.apache.catalina.startup.Catalina.stop(Catalina.java:616)
    at org.apache.catalina.startup.Catalina$CatalinaShutdownHook.run(Catalina.java:659)
获取数据库连接失败!
java.lang.NullPointerException
    at com.yxq.toolsbean.DB.getStm(DB.java:38)
    at com.yxq.toolsbean.DB.executeQuery(DB.java:69)
    at com.yxq.dao.LogonDao.getMaster(LogonDao.java:20)
    at com.yxq.servlet.IndexServlet.<clinit>(IndexServlet.java:68)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:25)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:122)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:918)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:899)
    at java.lang.reflect.Field.set(Field.java:657)
    at org.apache.catalina.loader.WebappClassLoader.clearReferences(WebappClassLoader.java:1615)
    at org.apache.catalina.loader.WebappClassLoader.stop(WebappClassLoader.java:1495)
    at org.apache.catalina.loader.WebappLoader.stop(WebappLoader.java:707)
    at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4534)
    at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:924)
    at org.apache.catalina.startup.HostConfig.undeployApps(HostConfig.java:1180)
    at org.apache.catalina.startup.HostConfig.stop(HostConfig.java:1151)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:313)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1086)
    at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1098)
    at org.apache.catalina.core.StandardEngine.stop(StandardEngine.java:448)
    at org.apache.catalina.core.StandardService.stop(StandardService.java:584)
    at org.apache.catalina.core.StandardServer.stop(StandardServer.java:744)
    at org.apache.catalina.startup.Catalina.stop(Catalina.java:616)
    at org.apache.catalina.startup.Catalina$CatalinaShutdownHook.run(Catalina.java:659)



这个是页面中显示的内容

程序代码:
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
    com.yxq.dao.FriendDao.queryFriend(FriendDao.java:47)
    com.yxq.servlet.IndexServlet.doPost(IndexServlet.java:52)
    com.yxq.servlet.IndexServlet.doGet(IndexServlet.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:694)
    org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:665)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:56)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.13




本人新手,错误一直调试不好,麻烦各位大大帮忙看看。急死人了~







[ 本帖最后由 swehenry 于 2009-12-23 23:56 编辑 ]
搜索更多相关主题的帖子: 数据库  JSP  

----------------解决方案--------------------------------------------------------
恳求各位大大帮忙找找错误,整了一天了
----------------解决方案--------------------------------------------------------
在线等~
----------------解决方案--------------------------------------------------------
private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Blog";
private final String userName = "sa";
private final String password = "sa";
private Connection con = null;
private Statement stm=null;

试一下把这些定义成public类型
----------------解决方案--------------------------------------------------------
修改了下,错误还是存在
----------------解决方案--------------------------------------------------------
根据错误提示,你的8080端口被其他程序占用了,Tomcat服务器启动不了

[ 本帖最后由 lampeter123 于 2009-12-25 14:56 编辑 ]
----------------解决方案--------------------------------------------------------
  相关解决方案