gpt4 book ai didi

java - 从 servlet 连接到数据库

转载 作者:行者123 更新时间:2023-11-29 08:49:20 24 4
gpt4 key购买 nike

我有jsp表单:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="./Styles/Site.css" type="text/css" />
<title>Create new customer</title>
</head>
<body>
<fieldset>
<legend>Create new customer</legend>
<form action="CreateCustomerServlet" method="GET">
// text of form ...
<input type="submit" value="Create customer" />
</form>
</fieldset>

</body>
</html>

转到CreateCustomerServlet -

package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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 javax.servlet.http.HttpSession;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import Model.Customer;
import Model.Gender;

/**
* Servlet implementation class CreateCustomerServlet
*/
@WebServlet("/CreateCustomerServlet")
public class CreateCustomerServlet extends HttpServlet {
// members ..

private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public CreateCustomerServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

// get the parameters from the jsp form ..
saveInDB() ;
}

private void saveInDB() throws ClassNotFoundException, SQLException {

Connection connection = null;
try {
String strServer = "jdbc:mysql://127.0.0.1:3306/testdb";
MysqlDataSource ds = new MysqlConnectionPoolDataSource();
ds.setServerName(strServer);
ds.setDatabaseName("testdb");
connection = ds.getConnection("root", "");
Statement statement = connection.createStatement();
statement.execute(" /** sql INSERT statement **/");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}
}

当它到达行 - connection = ds.getConnection("root", ""); 它抛出异常 - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对于输入字符串:“mysql:”'。

我与 MySql 一起使用,在 http://localhost/phpmyadmin/ 中,我有一个数据库 testdb ,其用户如快照中所示 -

enter image description here

并且两个用户的密码都是空的作为快照 -

enter image description here

最佳答案

您可以混合使用 2 种不同的方式来建立连接。

你可以这样做:

MysqlDataSource ds = new MysqlDataSource();
ds.setServerName("localhost");
ds.setPort("3306");
ds.setDatabaseName("testdb");
ds.setUser("root");
ds.setPassword("");
Connection connection = ds.getConnection();

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","");

顺便说一下,将连接设置放在属性文件中是一个很好的做法。您应该阅读这篇优秀的文章post与此相关article .

关于java - 从 servlet 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com