当前位置: 技术问答>java相关
小弟被一个问题搞得很郁闷,大家快来帮帮忙!
来源: 互联网 发布时间:2017-03-14
本文导语: 在此之前,已经用grant select on test.Name to root@localhost identified by '511125'和flush privileges语句授权. 原程序如下: /* * MysqlTest.java * * Created on December 6, 2002, 7:14 PM */ import java.io.*; import java.net.*; import javax.ser...
在此之前,已经用grant select on test.Name to root@localhost identified by '511125'和flush privileges语句授权.
原程序如下:
/*
* MysqlTest.java
*
* Created on December 6, 2002, 7:14 PM
*/
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
*
* @author root
* @version
*/
public class MysqlTest extends HttpServlet {
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/** Destroys the servlet.
*/
public void destroy() {
}
/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//output your page here
out.println("");
out.println("");
out.println("Mysql Test");
out.println("");
out.println(" A list of names from a
Mysql table");
out.println("");
out.println("First Name"+"Last Name");
String connectionURL="jdbc:mysql://localhost:3306/test";
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection=DriverManager.getConnection(connectionURL,"root", "511125");
statement=connection.createStatement();
resultSet=statement.executeQuery("SELECT * FROM Name");
while(resultSet.next()){
out.println(""+resultSet.getString("FirstName")
+""+resultSet.getString("LastName")+"");
}
if(resultSet!=null){
resultSet.close();
}
}
catch(ClassNotFoundException e){
System.err.println("Could't find the mm"+"database driver:"+e.getMessage());
}
catch(InstantiationException e){
System.err.println(e.getMessage());
}
catch(IllegalAccessException e){
System.err.println(e.getMessage());
}
catch(SQLException e){
System.err.println("SQL Problem:"+e.getMessage());
System.err.println("SQL State:"+e.getSQLState());
System.err.println("Vendor Error:"+e.getErrorCode());
}
finally{
try{
if(connection!=null){
connection.close();
}
}catch(SQLException e){
System.err.println(e.getMessage());
}
}
out.println("");
out.println("");
out.println("");
out.close();
}
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
在浏览器中输入http://localhost:8080/examples/servlet/MysqlTest 后,出现提示如下:'the requested service is not currently available.'.即使访问原有的程序(examples下的自带程序),也会出现这个问题.
而且我把小程序放在webapps/examples/WEB-INF/classes下,请问各位大师问题出在什么地方?
原程序如下:
/*
* MysqlTest.java
*
* Created on December 6, 2002, 7:14 PM
*/
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
*
* @author root
* @version
*/
public class MysqlTest extends HttpServlet {
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/** Destroys the servlet.
*/
public void destroy() {
}
/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//output your page here
out.println("");
out.println("");
out.println("Mysql Test");
out.println("");
out.println(" A list of names from a
Mysql table");
out.println("");
out.println("First Name"+"Last Name");
String connectionURL="jdbc:mysql://localhost:3306/test";
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection=DriverManager.getConnection(connectionURL,"root", "511125");
statement=connection.createStatement();
resultSet=statement.executeQuery("SELECT * FROM Name");
while(resultSet.next()){
out.println(""+resultSet.getString("FirstName")
+""+resultSet.getString("LastName")+"");
}
if(resultSet!=null){
resultSet.close();
}
}
catch(ClassNotFoundException e){
System.err.println("Could't find the mm"+"database driver:"+e.getMessage());
}
catch(InstantiationException e){
System.err.println(e.getMessage());
}
catch(IllegalAccessException e){
System.err.println(e.getMessage());
}
catch(SQLException e){
System.err.println("SQL Problem:"+e.getMessage());
System.err.println("SQL State:"+e.getSQLState());
System.err.println("Vendor Error:"+e.getErrorCode());
}
finally{
try{
if(connection!=null){
connection.close();
}
}catch(SQLException e){
System.err.println(e.getMessage());
}
}
out.println("");
out.println("");
out.println("");
out.close();
}
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
在浏览器中输入http://localhost:8080/examples/servlet/MysqlTest 后,出现提示如下:'the requested service is not currently available.'.即使访问原有的程序(examples下的自带程序),也会出现这个问题.
而且我把小程序放在webapps/examples/WEB-INF/classes下,请问各位大师问题出在什么地方?
|
输入 http://localhost:8080/
是否能看到index.htm页面呢!如果连这个页面也看不到,那一定是你的tomcat 或是resin装的有问题.
|
Up有分吗?