首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

新手提问;连接数据库如何就那么难?帮小弟我看看这个有关问题

2011-12-31 
新手提问连接数据库怎么就那么难??帮我看看这个问题。用eclipse建了个j2eeweb工程,里头就一个类和一张jsp

新手提问;连接数据库怎么就那么难??帮我看看这个问题。
用eclipse建了个j2ee   web工程,里头就一个类和一张jsp页面,读数据库:
----------------------------------------
DBConn.java:
package   com.test.DB;

import   java.sql.*;
import   java.io.*;
import   java.util.*;

public   class   DBConn  
{
private   Connection   conn=null;

public   Connection   getConnection()   throws   SQLException
{
String   Driver= "net.sourceforge.jtds.jdbc.Driver ";
String   URL= "jdbc:jtds:sqlserver://222.31.170.89:1433/bookshop ";
String   UserName= "sa ";
String   PassWord= " ";
Connection   conn=null;
try
{
Class.forName(Driver);
}
catch(ClassNotFoundException   e)
{
e.printStackTrace();
}

try
{
conn=DriverManager.getConnection(URL,UserName,PassWord);
}
catch(SQLException   e)
{
throw   e;
}
return   conn;
}

public   PreparedStatement   getPreparedStatement(String   sql)
{
PreparedStatement   ps=null;
try
{
conn=getConnection();
ps=conn.prepareStatement(sql);
return   ps;
}
catch(Exception   ee)
{
Close(ps);
ee.printStackTrace();
return   null;
}
}

public   void   release()
{
try
{
conn.close();
}
catch(SQLException   e)
{
e.printStackTrace();
}
}

public   static   int   Close(PreparedStatement   ps)
{
if(ps==null)
return   0;  
try
{
ps.close();
return   0;
}
catch(SQLException   e)
{
return   -1;
}
}
}
----------------------------------------------
testDBConn.jsp:
<%@   page   language= "java "   import= "java.util.* "   pageEncoding= "ISO-8859-1 "%>
<%@   page   import= "com.test.DB.* "   %>
<%@   page   import= "java.sql.* "   %>
<%@   page   import= "java.io.* "   %>
<%
String   path   =   request.getContextPath();
String   basePath   =   request.getScheme()+ ":// "+request.getServerName()+ ": "+request.getServerPort()+path+ "/ ";
%>

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">
<html>
    <head>
        <base   href= " <%=basePath%> ">
       
        <title> My   JSP   'testDBConn.jsp '   starting   page </title>
       
<meta   http-equiv= "pragma "   content= "no-cache ">
<meta   http-equiv= "cache-control "   content= "no-cache ">
<meta   http-equiv= "expires "   content= "0 ">        
<meta   http-equiv= "keywords "   content= "keyword1,keyword2,keyword3 ">
<meta   http-equiv= "description "   content= "This   is   my   page ">
<!--


<link   rel= "stylesheet "   type= "text/css "   href= "styles.css ">
-->

    </head>
    <body>
    <%
    DBConn   dbConn=new   DBConn();
    String   sql= "select   *   from   myorder ";
    try
    {
    PreparedStatement   ps=dbConn.getPreparedStatement(sql);
    ResultSet   rs=ps.executeQuery();
    while(rs.next())
    {
    out.println(rs.getString(0));
    }
    }
    catch(SQLException   e)
    {
    e.printStackTrace();
    }
    %>
    </body>
</html>
-----------------------------------------------
错误信息:

org.apache.jasper.JasperException:   Exception   in   JSP:   /testDBConn.jsp:34

31:       try
32:       {
33:       PreparedStatement   ps=dbConn.getPreparedStatement(sql);
34:       ResultSet   rs=ps.executeQuery();
35:       while(rs.next())
36:       {
37:       out.println(rs.getString(0));


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

java.lang.NullPointerException
org.apache.jsp.testDBConn_jsp._jspService(testDBConn_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


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

求助!!!!!!!!!!!!!!!!!!!!!!!!!!!!1


[解决办法]
1,检查你的数据库的名称是否一致
2,测测你的SQL Server的服务端口(1433)打开没有
[解决办法]

应该是这样把

try
{
Class.forName(Driver).newInstance();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
return null
}

热点排行