mysql - jdbc连接mysql到html

标签 mysql html jdbc

您好,我有以下文件可以将 mysql 数据库连接到 html 文件。但我无法连接它。谁能告诉我在哪里可以找到这些地点。 我应该用什么替换“jdbc:mysql://localhost/zulfiqar”才能在我的电脑上运行?我在哪里可以找到这个? 还有什么我必须改变才能让它在我的电脑上工作吗?这是我在互联网上找到的一段代码,我正在尝试使其工作,以便我可以理解如何去做,但我正在努力。 提前致谢!

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUserEnquiryForm extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
/**Process the HTTP Get request*/
public void doPost(HttpServletRequest req, 
HttpServletResponse res) throws ServletException,
IOException{
String connectionURL = "C:\Program Files(x86)\MySQL Server 5.0\bin\mysql.exe";
Connection connection=null;
ResultSet rs;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//get the variables entered in the form
String uId = req.getParameter("userId");
String fname = req.getParameter("firstname");
String sname = req.getParameter("surname");
String address1 = req.getParameter("address1");
String address2 = req.getParameter("address2");
String town = req.getParameter("town");
String county = req.getParameter("country");
String zipcode = req.getParameter("zipcode"); 
try {
// Load the database driver
Class.forName("org.gjt.mm.mysql.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection
(connectionURL, "root", "admin"); 
//Add the data into the database
String sql = 
"insert into emp_details values (?,?,?,?,?,?,?,?)";
PreparedStatement pst = 
connection.prepareStatement(sql);
pst.setString(1, uId);
pst.setString(2, fname);
pst.setString(3, sname);
pst.setString(4, address1);
pst.setString(5, address2);
pst.setString(6, town);
pst.setString(7, county);
pst.setString(8, zipcode);
int numRowsChanged = pst.executeUpdate();
// show that the new account has been created
out.println(" Hello : ");
out.println(" '"+fname+"'");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " 
+ e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " 
+ e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}

最佳答案

一些关于失败原因的额外信息可能会有用。

1) 是否无法建立套接字连接(意味着您的服务未运行),或者 2) 是否初始化驱动程序失败?我不熟悉你列出的那个。一个更常见的替代方案是“sun.jdbc.odbc.JdbcOdbcDriver”。 3) 您是否已连接并通过用户“root”和密码“admin”的身份验证失败?

关于mysql - jdbc连接mysql到html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523247/

相关文章:

php - 无法将 FORM 中的变量与 PHP 中的 MYSQL 记录匹配

php - 使用 DISTINCT 和 ORDER BY 进行 MySQL 查询

mysql - 根据条件连接两个表以及第二个表中的记录计数

javascript - 用于 JavaScript 的简单所见即所得 BBCode 编辑器?

java 程序无法进入 while 循环以获得唯一行结果集

php - 如何使用 PHP/MySQL 搜索其他变量的特定内容?

HTML5 + CSS3 下拉导航栏

javascript - 切换包含另一个 html 页面的 div

java - 使用beeline或java jdbc代码时,配置单元抛出错误

java jsp if语句