这是我的index.jsp:
的代码<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="org.apache.tomcat.jdbc.pool.DataSource"%>
<%@page import="org.apache.tomcat.jdbc.pool.PoolProperties"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div id="query">
<%
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
p.setDriverClassName("oracle.jdbc.OracleDriver");
p.setUsername("scott");
p.setPassword("tiger");
p.setMaxActive(100);
p.setInitialSize(10);
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
Connection con = null;
con = datasource.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select 777 from dual");
while (rs.next()) {
System.out.println(rs.getString(1));;
}
rs.close();
st.close();
%>
</div>
</body>
</html>
这是运行该代码的错误: 我不明白为什么这个代码运行oracle:
<%
String dcn = "oracle.jdbc.OracleDriver";
Class.forName(dcn);
String serverName = "localhost";
String portNumber = "1521";
String sid = "orcl";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stm = conn.createStatement();
String query = "SELECT * from emp";
ResultSet data = stm.executeQuery(query);
while (data.next()) {
String number = data.getString("EMPNO");
String name = data.getString("ENAME");
%> <%=number%> <%=name%> <br> <%
}
%>
尽管我正确地导入了驱动程序。我应该如何在mysql中执行此操作?尝试更换驱动程序,但也没有工作。
MYSQL代码:
<div id="query">
<%
String username = "j2ee";
String password = "j2ee123456";
String dbName = "testjava";
String dbHost = "localhost";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException msg) {
out.println("Error loading driver:" + msg.getMessage());
}
try {
String url = "jdbc:mysql://" + dbHost + ":3306/" + dbName;
Connection Conn = DriverManager.getConnection(url, username, password);
Statement Stmt = Conn.createStatement();
String query = "SELECT * FROM example_autoincrement";
ResultSet rs = Stmt.executeQuery(query);
while (rs.next()) {
int numExp = rs.getInt("id");
String nombre = rs.getString("data");
%> <%=numExp%> <%=nombre%> <%
}
} catch (SQLException e) {
String err1Msg = e.getMessage();
%>
<STRONG><EM> <%=err1Msg%> </EM></STRONG>
<%
}
%>
我在VPS上的错误 加载驱动程序时出错:com.mysql.jdbc.Driver找不到合适的jdbc驱动程序:mysql:// localhost:3306 / testjava
但是我在运行相同代码的XAMPP时没有收到此错误...
答案 0 :(得分:0)
解决:将mysql驱动程序放入WEB-INF / lib文件夹后不得不重启tomcat。