解决方案:
我添加了此代码
Class.forName("com.mysql.jdbc.Driver");
brfore
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
谢谢大家回复我的问题
====================
我有问题,我尝试使用servlet将数据插入mysql数据库,但我无法访问MySQL
1-这是sign_up.jsp页面中的“表单操作”:
<form action="RegisterUser" method="post">
<td><input type="submit" value="Submit"></td>
</form>
2-这是RegisterUser.java servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
@WebServlet(urlPatterns = {"/RegisterUser"})
public class RegisterUser extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
Statement s = (Statement) con.createStatement();
String name = "Hassan3";
int phone = 123456;
String insert = "INSERT INTO test VALUES ('\" + name + \"', \" + phone + \")";
s.executeUpdate(insert);
s.close();
con.close();
}catch(Exception e){
throw new SecurityException("Class not found " + e.toString());
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(RegisterUser.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(RegisterUser.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
3-异常结果:
HTTP Status 500 - Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test
type Exception report
message Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.SecurityException: Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test
RegisterUser.processRequest(RegisterUser.java:66)
RegisterUser.doPost(RegisterUser.java:173)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.
4-但是当我使用相同的代码但在java文件中“没有servlet或web应用程序”时它正常工作:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Test {
public static void main(String[] args){
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
Statement s = (Statement) con.createStatement();
String name = "Hassan4";
int phone = 8985895;
String insert = "INSERT INTO test VALUES ('" + name + "', " + phone + ")";
s.executeUpdate(insert);
s.close();
con.close();
System.out.println("done");
}catch(Exception e){
throw new SecurityException("Class not found " + e.toString());
}
}
}
那么servlet有什么问题? 为什么代码适用于java app。但它不能与网络应用程序一起使用。
答案 0 :(得分:2)
您将获得Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test
这意味着当您从Web应用程序运行它时,JRE无法在Classpath中找到Class。
如果此代码在您的Standalone中有效,则意味着您需要在某处包含c om.mysql.jdbc.Driver
类的JAR文件(所谓的 JDBC 驱动程序)。这个JAR需要在Tomcat中可见。因此,我建议将mysql-jdbc.jar
放在项目的/WEB-INF/lib
目录的物理位置。
或者,您可以使用
添加第三方库,如JDBC驱动程序右键单击项目名称 - &gt;特性
来自NetBeans
IDE
然后重启Tomcat应该可以工作。
其次,你不需要
import com.mysql.jdbc.Driver;
在你的Servlet中。
答案 1 :(得分:1)
David是对的,我想添加,您也可以通过将jar文件粘贴到java的安装文件夹中来安装驱动程序。
\Program Files\Java\jre7\lib\ext
嗯,我不喜欢mysql并且总是使用Mssql和Windows服务器2008.这是我用的代码,我可能是你的答案,因为mysql连接的工作方式与sql几乎相同。
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName="+database+";user="+user+";password="+password);
答案 2 :(得分:0)
首先,您应该将其放入持久层。
1)确保您的JDBC驱动程序已就绪。将其复制到类路径中,例如/ WEB-INF / lib目录。链接:MySQL JDBC Driver Download Page
2)检查连接字符串:jdbc:mysql://<server>:<port>/<database>
,看起来缺少端口。试试jdbc:mysql://127.0.0.1:3306/test