说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

如何跟msslq数据库连接?

编辑:说三道四文库 发布时间:2018-04-26 09:42
HTML文档下载 WORD文档下载 PDF文档下载
各位大虾,请问jsp中如何跟msslq数据库连接,请求详细代码?
我用的是jbuild9.0+Tomcat 4.1+mssql server 2000
怎么都没人回答
!自己up一下!
我把原代码给你
改一下jdbc那里就可以了
// Java Document
import java.sql.*;
public class sql_data
{
String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
//String ConnStr="jdbc:odbc:ClassDB,sa,sa";
Connection conn=null;
ResultSet rs=null;
//String id,password;
public sql_data()
{
    try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("sql_data():"+e.getMessage());
}
}
public void Insert(String sql)
{
try{
conn=DriverManager.getConnection("jdbc:odbc:ClassDB","sa","sa");
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
   stmt.close();
}
catch(SQLException ex)
{
System.err.println("sql_data.Insert:"+ex.getMessage());
}
}
public void Update(String sql)
{
try{
conn=DriverManager.getConnection("jdbc:odbc:ClassDB","sa","sa");
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
   stmt.close();
}
catch(SQLException ex)
{
System.err.println("sql_data.Update:"+ex.getMessage());
}
}
public ResultSet Query(String sql)
{
rs=null;
try{
conn=DriverManager.getConnection("jdbc:odbc:ClassDB","sa","sa");
Statement stmt=conn.createStatement();
stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("sql_data.Query:"+ex.getMessage());
}
return rs;
}
public void Delete(String sql)
{
try{
conn=DriverManager.getConnection("jdbc:odbc:ClassDB","sa","sa");
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
  stmt.close();
}
catch(SQLException ex)
{
System.err.println("sql_data.Delete:"+ex.getMessage());
}
}
public void Disconnection()
{
try{
if(conn!=null) conn.close();
if(rs!=null) rs.close();
}
catch(Exception ex)
{
System.err.println("sql_data.Disconnection:"+ex.getMessage());
}
}
}
import java.sql.* ;
import javax.sql.*;


("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();    
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs"; 
String user="sa"; 
String password="123456"; 
Connection conn= DriverManager.getConnection(url,user,password);
我试一下!成功马上结帖!
sagittarius1979(射手爱狮子) ( ) 
我用你的程序出先了以下错误,不知道是什么原因:
type Exception report

message 

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

exception 

org.apache.jasper.JasperException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:589)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)
at java.lang.Thread.run(Thread.java:536)


root cause 

javax.servlet.ServletException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:536)
at org.apache.jsp.ceshi_jsp._jspService(ceshi_jsp.java:57)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:589)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)
at java.lang.Thread.run(Thread.java:536)

同意 sagittarius1979(射手爱狮子) 
不过,你要把那三个文件放在yourapp\WEB-INF\lib下
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘