Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从servlet连接到数据库_Java_Mysql_Jsp_Servlets_Phpmyadmin - Fatal编程技术网

Java 从servlet连接到数据库

Java 从servlet连接到数据库,java,mysql,jsp,servlets,phpmyadmin,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&

我有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>
当它到达第行时-
connection=ds.getConnection(“根”,“根”)
它引发异常-
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法加载连接类,因为存在底层异常:“java.lang.NumberFormatException:对于输入字符串:“mysql:”。

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

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


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

String strServer = "127.0.0.1";
ds.setServerName(strServer);
您必须设置端口号:

 dataSource.setPort(3306);

您可以混合使用两种不同的方法来建立连接

你可以做:

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

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

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","");