我在Eclipse里用Servlet 连接My sql时出现以下错误不知为何?谢谢/*type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Io 异常: The Network Adapter could not establish the connection
com.tarena.serv.resource.JdbcServlet.doGet(JdbcServlet.java:27)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.*/
com.tarena.serv.resource.JdbcServlet.doGet(JdbcServlet.java:27)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.*/
解决方案 »
- 在线阅读加锁的功能 或者JAVA悲观锁5
- 通过jdk注解生成的webservice ,怎么取得客户端的IP和MAC?
- 求助!“java文本框编辑器”功能代码或者js或者例子
- 关于数组的问题
- 在struts多模块开发中 子模块页面struts标签html:form的问题
- 实际项目中,Hibernate的hbm.xml文件大家倾向于放置在什么位置
- 文件上传问题100分
- 使用HttpSessionBindingListener,当浏览器关闭人数统计才加1?
- hibernate+struts单独用这两个框架没问题,但和在一起就这样,用户名密码都没问题,还望各位帮帮忙
- 如何连接数据库的问题
- java抓取被拒绝
- JSP 设置时间显示的时区.
2 MySQL 不再你指定的主机上
3 查一下你的网络吧,也许你根本无法连接那台机器的MySQL, 比如,打开了防火墙。
内容:显示user信息listpackage com.tarena.serv.resource;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;public class JdbcServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
con = getConnectionByMysql();
System.out.println("OK");
st = con.createStatement();
rs = st.executeQuery("select * from user_tbl");
showUsers(response, rs);
}catch(Exception e)
{
e.printStackTrace();
throw new ServletException(e.getMessage());
}finally
{
close(rs, st, con);
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
private Connection getConnection() throws Exception
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.23:1521:tarena", "sd0708", "sd0708");
}catch(ClassNotFoundException e)
{
System.out.println("can not found oracle.jdbc.driver.OracleDriver");
throw e;
}catch(SQLException e)
{
System.out.println("can not connect databse using jdbc:oracle:thin:@192.168.0.23:1521:tarena sd0708/sd0708");
throw e;
}
}
// 建立DB连接
private Connection getConnectionByMysql()throws Exception
{
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/test";
String username="root";
String pwd="123";
return DriverManager.getConnection(url,username,pwd);
}catch(ClassNotFoundException e){
System.out.println("can not found com.mysql.jdbc.Driver");
throw e;
}catch(SQLException e)
{
System.out.println("can not connect databse using jdbc:mysql://127.0.0.1:3306/test root/123");
throw e;
}
}
private void showUsers(HttpServletResponse response, ResultSet rs) throws Exception
{
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>user list</title></head>");
out.println("<body>");
out.println("<h3 align=\"center\">User List</h3>");
out.println("<hr>");
out.println("<table align=\"center\" border=\"1\" cellpadding=\"3\" width=\"650\">");
out.println("<tr>");
out.println("<th>no</th>");
out.println("<th>username</th>");
out.println("<th>gender</th>");
out.println("<th>hobbies</th>");
out.println("<th>province</th>");
out.println("</tr>");
int no = 1;
while(rs.next())
{
out.println("<tr>");
out.println("<td align=\"center\">" + no + "</td>");
out.println("<td align=\"center\">" + rs.getString("uname") + "</td>");
out.println("<td align=\"center\">" + rs.getString("gender") + "</td>");
out.println("<td align=\"center\">" + rs.getString("hobbies") + "</td>");
out.println("<td align=\"center\">" + rs.getString("province") + "</td>");
out.println("</tr>");
no++;
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
private void close(ResultSet rs, Statement st, Connection con)
{
try
{
rs.close();
}catch(Exception e)
{
}
try
{
st.close();
}catch(Exception e)
{
}
try
{
con.close();
}catch(Exception e)
{
}
}
}
或者用开始菜单里面的 mysql command line client 看看!