将Eclipse与MySQL集成

时间:2015-12-15 05:58:57

标签: java mysql eclipse mysql-workbench

在将eclipse中开发的Web应用程序与mysql数据库(mysql workbench)集成时,我遇到了一个问题。

下面的

是我使用的jsp文件和数据库名称。

数据库用户名 - root 密码 - 1234

的Login.jsp



<%-- 
    Document   : Login
    Created on : 28 Feb, 2015, 8:50:26 AM
    Author     : Lahaul Seth
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Demo with JSP</title>
    </head>
    <body>
        <form method="post" action="userdetail.jsp">
            <center>
            <table border="1" cellpadding="5" cellspacing="2">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Username</td>
                        <td><input type="text" name="username" required/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="password" required/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Login" />
                            &nbsp;&nbsp;
                            <input type="reset" value="Reset" />
                        </td>                        
                    </tr>                    
                </tbody>
            </table>
            </center>
        </form>
    </body>
</html>
&#13;
&#13;
&#13;

userdetail.jsp

&#13;
&#13;
<%@ page language="java" %>
<%@ page import="org.owasp.esapi.ESAPI" %>
<%@ page import="org.owasp.esapi.codecs.Codec" %>
<%@ page import="org.owasp.esapi.codecs.MySQLCodec" %>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%! Connection con= null; %> 
<%! Statement stmt= null; %> 
<%! ResultSet rs= null; %> 
<%! MySQLCodec mc= null; %> 

<html> 
<head><title>List Users</title></head> 
<% 
String uname = request.getParameter("uname");
String pass =  request.getParameter("pass");

String uid = request.getParameter("uid");

try { 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
}catch(ClassNotFoundException ce){out.println(ce);} 

try{ 
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "1234"); 
stmt = con.createStatement(); 
String sql = "select * from userdetail where id = "+Integer.parseInt(uid);
//out.println(sql);
rs = stmt.executeQuery(sql);
%>

<body>
<br>
<br>
<div align="center"><b>You have Successfully Logged In Mr/Ms<%=uname%>, Your Details are :-</b></div>
<br>
<br>
<br>
<form action="view.jsp">
<table align="center">
<% while(rs.next()) {%>
	<tr><td>First Name</td><td><input type=text name="fname" value='<%=rs.getString("firstname")%>' ></td></tr>
	<tr><td>Last Name</td><td><input type=text name="lname" value='<%=rs.getString("lastname")%>'></td></tr>
	<tr><td>Email</td><td><input type=text name="email" value='<%=rs.getString("email")%>'></td></tr>
	<tr><td>Phone</td><td><input type=text name="phone" value='<%=rs.getString("phone")%>'></td></tr>
	<tr><td>Address</td><td><input type=text name="address" value='<%=rs.getString("address")%>'></td></tr>
<% 
} 
rs.close(); 
stmt.close(); 
con.close(); 

}catch(SQLException exception){ 
	//out.println("<!--"); 
	StringWriter sw = new StringWriter(); 
	PrintWriter pw = new PrintWriter(sw); 
	exception.printStackTrace(pw); 
	out.print(sw); 
	sw.close(); 
	pw.close(); 
	//out.println("-->"); 
} 
%> 
</table>
</form>
</body> 
</html> 
&#13;
&#13;
&#13;

auth.jsp

&#13;
&#13;
<%@ page language="java" %>
<%@ page import="org.owasp.esapi.ESAPI" %>
<%@ page import="org.owasp.esapi.codecs.Codec" %>
<%@ page import="org.owasp.esapi.codecs.MySQLCodec" %>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%! Connection con=null; %> 
<%! Statement stmt= null; %> 
<%! ResultSet rs= null; %> 
<%! MySQLCodec mc= null; %> 

<html> 
<head><title>This is a Jdbc Example</title></head> 
<body> 

<% 
String uname = request.getParameter("uname");
String pass =  request.getParameter("pass");

try { 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
}catch(ClassNotFoundException ce){out.println(ce);} 

try{ 
con = DriverManager.getConnection("jdbc:mysql://localhost/test:3306","root", "1234"); 
stmt = con.createStatement(); 
//String sql = "select * from user_detail where uname='" + uname +"' and pass='" + pass + "'";

mc = new MySQLCodec(0);

String sql = "SELECT * FROM userdetail WHERE uname = '" + ESAPI.encoder().encodeForSQL( mc, uname) + "' and pass = '" + ESAPI.encoder().encodeForSQL( mc, pass) +"'";

out.println(sql);
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
	boolean loggedIn = true;
	//response.sendRedirect("userdetail.jsp?uid=1");
	out.println("Successfully logged in");
} else {
	//response.sendRedirect("login.jsp");
	out.println("Username and/or password not recognized");
}
rs.close(); 
stmt.close(); 
con.close(); 

}catch(SQLException exception){ 
//out.println("<!--"); 
StringWriter sw = new StringWriter(); 
PrintWriter pw = new PrintWriter(sw); 
exception.printStackTrace(pw); 
out.print(sw); 
sw.close(); 
pw.close(); 
//out.println("-->"); 

} 



%> 
</body> 
</html> 
&#13;
&#13;
&#13;

MySQL datatbase

Error page

1 个答案:

答案 0 :(得分:0)

嗯,这听起来是一个类路径问题。您能否查看WEB-INF / classes中存在的MySQLCodec类,或者在WEB-INF / lib路径中的任何jar中查看。

您可以仔细检查tomcat war目录。它应该在下面的示例文件夹结构中。

Code Signing Identity