java.sql.SQLException: No suitable driver found for jdbc:mysql:/localhost/XXX?还是没有解决

来源:互联网 发布:淘宝代购网店 注册 编辑:程序博客网 时间:2024/04/30 10:22
package servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class Insert extends HttpServlet {
/**
* Constructor of the object.
*/
public Insert() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>

* This method is called when a form has its tag value method equals to get.

* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}


/**
* The doPost method of the servlet. <br>

* This method is called when a form has its tag value method equals to
* post.

* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean flag;


try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("驱动包加载失败。");
System.exit(0);
}
// 建立与数据库的连接
Connection con=DriverManager.getConnection("jdbc:mysql:/localhost/org.lyy"
,"root","123456");
// 发送语句
Statement stmt = con.createStatement();
request.setCharacterEncoding("UTF-8");
String SID = request.getParameter("SID");
String SName = request.getParameter("SName");
String SSex = request.getParameter("SSex");
String SNum = request.getParameter("SNum");
String SPhone = request.getParameter("SPhone");
String SSubject = request.getParameter("SSubject");
String SCollege = request.getParameter("SCollege");
String PlaceOfBirth = request.getParameter("PlaceOfBirth");
String SPhoto = request.getParameter("SPhoto");
String sql = "select * from Student where SID='" + SID + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = false;
} else {
sql = "INSERT INTO Student(SID,SName,SSex,SNum ,SPhone,SSubject,SCollege,PlaceOfBirth,SPhoto)  "
+ "VALUES ('"
+ SID
+ "','"
+ SName
+ "','"
+ SSex
+ "','"
+ SNum
+ "','"
+ SPhone
+ "','"
+ SSubject
+ "','"
+ SCollege
+ "','"
+ PlaceOfBirth
+ "','"
+ SPhoto + "')";
// 更新数据库,每一次对数据库进行操作后,都记得更新数据库;
stmt.executeUpdate(sql);
flag = true;
}
response.setContentType("text/html;charset=UTF-8"); // 定义页面显示类型与编码方式
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>添加成功</TITLE>");
out.println("</HEAD>");
out.println("  <BODY>");
out.println("<h4>");
if (flag == true) {
out.println("添加成功,可以继续添加... ...");
String trans = "<meta http-equiv=\"Refresh\" content=\"2;url=../InsertStudent.jsp\">";
out.println(trans);
} else {
out.println("添加的用户已经存在,请重新添加... ...");
String trans = "<meta http-equiv=\"Refresh\" content=\"2;url=../InsertStudent.jsp\">";
out.println(trans);
}
out.println("</h4>");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
// 记住要close掉创建的对象和顺序
stmt.close();
con.close();


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* Initialization of the servlet. <br>

* @throws ServletException
*             if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}


网上貌似没有正确的

0 0
原创粉丝点击