Class.forName 连接 mysql 驱动一直抛出 ClassNotFoundException 异常解决方案


Class.forName 连接 mysql驱动一直抛出 ClassNotFoundException 异常,解决方法如下:

一、将  mysql-connector-java-5.1.47.jar 复制到 WebContent\WEB-INF\lib 目录中。如下:
  C:\Software\java\workspaces\web1\myweb1\WebContent\WEB-INF\lib\mysql-connector-java-5.1.47.jar
  
二、右键项目 - Properties - Java Build Path - Libraries - Add JARs - JAR Selection - 
      找到  mysql-connector-java-5.1.47.jar - OK - Apply - OK

三、连接 MySQL 正常,全部下面代码。 

​
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<table border="1">
<% 
String url = "jdbc:mysql://127.0.0.1:3306/mysqldbname?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "root";
try{
	Class.forName("com.mysql.jdbc.Driver");
	
	Connection con = DriverManager.getConnection(url,user,password);
	if ( con.isClosed() ) {
		out.println(  "failed to connect the database");
		return;
	}
	Statement statement = con.createStatement();
	String sql = "SELECT * FROM `users`";
	ResultSet rs = statement.executeQuery(sql);
	
	String category = null;
       String id = null;
       String name = null;
       String value = null;
       while( rs.next() ){
       	category = rs.getString("category");
           id = rs.getString("id");
           name = rs.getString("name");
           value = rs.getString("value");
           out.println("<tr>");
           out.println("<td>" + id + "</td>");
           out.println("<td>" + name + "</td>");
           out.println("<td>" + value + "</td>");
           out.println("<td>" + category + "</td>");
           out.println("</tr>");
       }
       
	rs.close();
	con.close();
	out.println( "succeeded connecting to the database");
}catch(SQLException e){
	e.printStackTrace();
}
%>
</table>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值