下面是我的示例代码:
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
/* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is usermaster. */
String connectionURL = "jdbc:mysql://localhost/ekoh";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "");
// check weather connection is established or not by isClosed() method
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
我的数据库名称是“ekoh”,我使用的是没有密码的root帐户。。我仍然不知道为什么它到现在还不工作。。你能给予我一些替代代码运行吗?:)n.b.我用Tomcat和MySql在XAMPP中编写JSP。
1条答案
按热度按时间c90pui9n1#
需要做的是download the MySQL connector jar file,然后将其放在项目文件夹中的文件夹路径
/WEB-INF/lib
内。