<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
//student为你的数据库的
String user="henson";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from student";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
用这个可以连接数据库输出数据,打了补丁sp3,放好了驱动文件在tomcat/common/lib中
package student;
import java.sql.*;
public class conn {
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:student";
Connection conn=null;
ResultSet rs=null; public conn(){
try{
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
}
这个可以连接到数据源,没有出现提示:[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
究竟是什么问题呢? 在线等最后一个Bebug:
Thread [http-8080-1] (Suspended (exception NullPointerException))
ErrorDispatcher.createJavacError(String, Node$Nodes, StringBuffer, int, JspCompilationContext) line: 526
JspServletWrapper.handleJspException(Exception) line: 453
JspServletWrapper.service(HttpServletRequest, HttpServletResponse, boolean) line: 371
JspServlet.serviceJspFile(HttpServletRequest, HttpServletResponse, String, Throwable, boolean) line: 315
JspServlet.service(HttpServletRequest, HttpServletResponse) line: 265
JspServlet(HttpServlet).service(ServletRequest, ServletResponse) line: 803
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 269
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 188
StandardWrapperValve.invoke(Request, Response) line: 210
StandardContextValve.invoke(Request, Response) line: 174
StandardHostValve.invoke(Request, Response) line: 127
ErrorReportValve.invoke(Request, Response) line: 117
StandardEngineValve.invoke(Request, Response) line: 108
CoyoteAdapter.service(Request, Response) line: 151
Http11AprProcessor.process(long) line: 834
Http11AprProtocol$Http11ConnectionHandler.process(long) line: 640
AprEndpoint$Worker.run() line: 1286
ThreadWithAttributes(Thread).run() line: 595
String url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
//student为你的数据库的
String user="henson";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from student";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
用这个可以连接数据库输出数据,打了补丁sp3,放好了驱动文件在tomcat/common/lib中
package student;
import java.sql.*;
public class conn {
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:student";
Connection conn=null;
ResultSet rs=null; public conn(){
try{
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
}
这个可以连接到数据源,没有出现提示:[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
究竟是什么问题呢? 在线等最后一个Bebug:
Thread [http-8080-1] (Suspended (exception NullPointerException))
ErrorDispatcher.createJavacError(String, Node$Nodes, StringBuffer, int, JspCompilationContext) line: 526
JspServletWrapper.handleJspException(Exception) line: 453
JspServletWrapper.service(HttpServletRequest, HttpServletResponse, boolean) line: 371
JspServlet.serviceJspFile(HttpServletRequest, HttpServletResponse, String, Throwable, boolean) line: 315
JspServlet.service(HttpServletRequest, HttpServletResponse) line: 265
JspServlet(HttpServlet).service(ServletRequest, ServletResponse) line: 803
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 269
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 188
StandardWrapperValve.invoke(Request, Response) line: 210
StandardContextValve.invoke(Request, Response) line: 174
StandardHostValve.invoke(Request, Response) line: 127
ErrorReportValve.invoke(Request, Response) line: 117
StandardEngineValve.invoke(Request, Response) line: 108
CoyoteAdapter.service(Request, Response) line: 151
Http11AprProcessor.process(long) line: 834
Http11AprProtocol$Http11ConnectionHandler.process(long) line: 640
AprEndpoint$Worker.run() line: 1286
ThreadWithAttributes(Thread).run() line: 595
解决方案 »
- java web开发学习
- Resin 2.1.17 搭建网站程序503错误
- JSP页面静态化
- Tomcat控制台中总是出现:ParserUtils: warning org.xml.sax.SAXParseException:问题
- jsp IE 代理
- Hibernate级联删除
- 小女子初来驾到,求大神帮忙~
- java servlet 中 生成highchart图片的问题
- 在使用Weblogic 7的时候,更新一个class文件后,再次运行,Weblogic 抛出一大堆异常,不知为何。。。。
- 请推荐一些jsp的经典技术书籍??
- 2个关于JSP域名的问题
- VAILDATOR(新手问题).............
try {
Class.forName(dbDriver);
con=DriverManager.getConnection(url,user,password);
}catch(Exception e){
System.out.println("找不到驱动程序");
}
return con;
}
这样
private String user="sa";
private String password="123";
private Connection con=null;应该OK了
你这个conn什么时候让他指向一个对象了。nullpointer 啊
>>我要写类,就要用到这个String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";你的理解错误的,对于不同的数据库,需要使用该数据库厂商提供的JDBC驱动包,通过该驱动包,你才能链接到相应的数据库。jeffysky1(天天向上)
给你回答的已经很完善了,所以参考他的答案,你就可以获取你需要的链接了。