>데이터 베이스 >MySQL 튜토리얼 >java.sql.SQLException: No suitable driver found for jdbc:mys_MySQL

java.sql.SQLException: No suitable driver found for jdbc:mys_MySQL

WBOY
WBOY원래의
2016-06-01 13:09:571147검색

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.

*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet.


* 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(""-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("");
out.println("  

A Servlet");
out.println("  ");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("  ");
out.println("");
out.flush();
out.close();
}


/**
* The doPost method of the servlet.


* 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(""-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("");
out.println("  添加成功");
out.println("");
out.println("  ");
out.println("

");
if (flag == true) {
out.println("添加成功,可以继续添加... ...");
String trans = "";
out.println(trans);
} else {
out.println("添加的用户已经存在,请重新添加... ...");
String trans = "";
out.println(trans);
}
out.println("

");
out.println("  ");
out.println("");
out.flush();
out.close();
// 记住要close掉创建的对象和顺序
stmt.close();
con.close();


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


/**
* Initialization of the servlet.


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


}


网上貌似没有正确的

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.