从servlet连接到DB

时间:2012-07-29 13:16:13

标签: java mysql jsp servlets phpmyadmin

我有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: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "mysql:"'.

我使用MySql,在http://localhost/phpmyadmin/我有一个数据库testdb,其中的用户就像在快照中一样 -

enter image description here

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

enter image description here

2 个答案:

答案 0 :(得分:1)

服务器名称必须类似于localhost127.0.0.1而不是jdbc:mysql://127.0.0.1:3306/testdb

String strServer = "127.0.0.1";
ds.setServerName(strServer);

,您必须设置端口号:

 dataSource.setPort(3306);

答案 1 :(得分:1)

您可以通过两种不同的方式建立连接。

你可以这样做:

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相关。