我的项目目前由本地托管的一组JSP页面组成,这些页面连接到mySQL数据库,也就是本地数据库。
我正在尝试验证使用数据库进入网站的用户。然而,问题在于两者之间的联系。
在验证JSP页面中,我有以下代码:
<%
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String server = "jdbc:mysql://localhost:3306/";
String database = "my_db";
String user = "user";
String pass = "password";
try {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser(user);
dataSource.setPassword(pass);
dataSource.setServerName(server);
dataSource.setDatabaseName(database);
connection = dataSource.getConnection();
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/?user=user&password=password");
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//connection = DriverManager.getConnection(server/*+database*/+"?user="+user+"&password="+pass);
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//connection=DriverManager.getConnection(server/*+database*/,user,pass);
if(!connection.isClosed()){
//do stuff
}
}catch(Exception ex){
out.println("Cannot connect to database.<br>"+ex);
}finally{//Release Resources
if(resultSet!=null){
try{resultSet.close();}catch(SQLException sqlEx){}
resultSet = null;
}
if(statement != null){
try{statement.close();}catch(SQLException sqlEx){}
statement = null;
}
if(!connection.isClosed()){
try{connection.close();}catch(SQLException sqlEx){}
connection = null;
}
}
%>我尝试过各种连接方法,但似乎没有一种有效。
第一个(Datasource,没有注释)用一个java.lang.NullPointerException来打我。页面输出:http://prntscr.com/ff37x9
第二个(单String DriverManager连接)连接,但不识别数据库。但是,当我添加my_db时,它告诉我它没有找到任何名为"my_db“的数据库。
第三和第四是相同的第二。
我不明白什么是故障。我应该意识到其他的事情吗?
编辑:这个页面只需要连接,因为站点的其余部分是通过Java的session对象来管理的。
谢谢您抽时间见我。
发布于 2017-06-02 16:19:45
非常感谢@Krismorte的帮助。
可以通过在工作台中键入SELECT DATABASES;来找到数据库名称。
我使用的架构与我需要访问的数据库的名称相同。
也就是“dip_db_schm”
在我的例子中,连接条款是:
connection=DriverManager. getConnection("jdbc:mysql://localhost:3306/dip_db_schm","user","password");
发布于 2017-06-02 13:54:41
通常,数据源是通过JNDI为JEE应用程序配置的。
您可以检查本题
如果您没有机会编写java代码或使用JNDI,并且只能使用JSP,请考虑至少在SQL中使用JSTL标记库。
您可以配置数据源并执行如下示例中的操作:
<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>
<html>
<head>
<title>JSTL sql:query Tag</title>
</head>
<body>
<sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost/TEST"
user = "root" password = "pass123"/>
<sql:query dataSource = "${snapshot}" var = "result">
SELECT * from Employees;
</sql:query>
<table border = "1" width = "100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var = "row" items = "${result.rows}">
<tr>
<td> <c:out value = "${row.id}"/></td>
<td> <c:out value = "${row.first}"/></td>
<td> <c:out value = "${row.last}"/></td>
<td> <c:out value = "${row.age}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html> 惟一需要的是将JSTL标记库添加到应用程序类路径中,例如,将JSTL罐放在具有MySQL驱动程序jar的文件夹中。
发布于 2017-06-02 13:56:51
尝试用这种方式直接在URL连接上获得与数据库的连接
String driver = "com.mysql.jdbc.Driver";
String connection = "jdbc:mysql://localhost:3306/YourDBName";
String user = "root";
String password = "root";
Class.forName(driver);
Connection con = DriverManager.getConnection(connection, user, password);https://stackoverflow.com/questions/44330350
复制相似问题