Unable to compile class for JSP error tomcat,mysql - java

I'm trying to upload my jsp project to tomcat.
Under picture is structure of my jsp project.
and when in insert.html's function trying to use insert.jsp, under error evoke.
this code is only insert data to mysql server.
DBConnection.java :
package com.exam;
import java.sql.*;
public class DBConnection {
public static Connection getCon() throws SQLException {
// TODO Auto-generated method stub
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/testdb";
con=DriverManager.getConnection("url", "root", "kcclab");
System.out.println("DB 연결 완료");
con.close();
return con;
}
catch(ClassNotFoundException cnfe){
System.out.println("연결이 안되네요..."+cnfe.getMessage());
return null;
}
}
}
insert.jsp :
<%#page import="java.sql.SQLException" %>
<%#page import="java.sql.PreparedStatement" %>
<%#page language="java" contentType="text/html; charset=euc-kr"
pageEncoding="euc-kr" %>
<%#page import="java.sql.Connection" %>
<%#page import="com.exam.DBConnection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("euc-kr");
String id=request.getParameter("ID");
String pwd=request.getParameter("pwd");
Connection con=null;
PreparedStatement pstmt=null;
String sql="insert into members vlaues(?,?,sysdate)";
int n=0;
try{
con=DBConnection.getCon();
pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
n=pstmt.executeUpdate();
}
catch(SQLException se){
System.out.println(se.getMessage());
}
finally{
try{
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
}
catch(SQLException se){
System.out.println(se.getMessage());
}
}
%>
<script type="text/javascript">
if(<%=n%>>0){
alert("���������� ȸ�����ԵǾ����ϴ�.");
location.href="../index.html";
}
else{
alert("ȸ�����Կ� �����߽��ϴ�.");
history.go(-1);
}
</script>
</body>
</html>
insert.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>Insert title here</title>
<style type="text/css">
#regbox{width:300px;}
#regbox label{display:block; width:100px; float:left; }
</style>
</head>
<body>
<form method="post" action="insert.jsp">
<fieldset id="regbox">
<legend>ȸ������</legend>
<label for="id">���̵�</label>
<input type="text" name="id"/><br/>
<label for="pwd">��й�ȣ</label>
<input type="password" name="pwd"/><br/>
<input type="submit" value="����">
<input type="reset" value="���"/>
</fieldset>
</form>
</body>
</html>
How Can I avoid this error?

As per my understanding this may happen when you are trying to import the class which is not present under the class path.
Can you please have a look at on your tomcat webapps directory ??
The file DBConnection.class just may not be present under WEB-INF/classes folder.

Related

Resultset rs cannot be resolved

very recently i started learning Java Technology right now I'm working on Servlets while i fetch the data from data base it's not resolving resultset rs
here I attached my code
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<script
type="text/javascript" language="javascript">
javascript:window.history.forward(1);
</script>
<%
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Expires","0");
response.setDateHeader("Expires",-1);
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
update
display
logout
<script>
<%
try{
ResultSet rs=null;
String mail=(String)session.getAttribute("mail");
String sql="select * from tempregister where mail=?";
Class.forName("com.ibm.db2.DB2Driver");
Connection con=DriverManager.getConnection("jdbc:db2://localhost:50000/TEMP", "tarun", "12347890");
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, mail);
rs=ps.executeQuery();
}catch(Exception e){
}
%>
<%
while(rs.next())
{
%>
<%=rs.getString(4)%>
<%
}
}
catch(Exception e)
{
e.printStackTrace();
}%>
</script>
</a>
</body>
</html>
please help me out.. thanks
Your code should be changed at least:
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<script
type="text/javascript" language="javascript">
javascript:window.history.forward(1);
</script>
<%
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Expires","0");
response.setDateHeader("Expires",-1);
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
update
display
logout
<%
ResultSet rs = null;
try {
String mail = (String) session.getAttribute("mail");
String sql = "select * from tempregister where mail=?";
Class.forName("com.ibm.db2.DB2Driver");
Connection con = DriverManager.getConnection("jdbc:db2://localhost:50000/TEMP", "tarun", "12347890");
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, mail);
rs = ps.executeQuery();
while (rs.next()) {
%>
<%=rs.getString(4)%>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>

Incorrect output,shows Class.forName as output

The code shows me the output as "com.mysql.jdbc.Driver"
Is it because of the database or something? I am using JSP here and trying to verify details of users.
<%#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>
<%#page import="java.sql.*"%>
<%#page import="javax.sql.*" %>
<%
try{
String username=request.getParameter("user");
String password=request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/tyit1", "root", "sphinx1");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+username+"' and password='"+password+"'");
if(rs.next())
{
String user=rs.getString("username");
String pass=rs.getString("password");
}
else{
out.print("Invalid Credentials!");
}
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
</body>
</html>

Validate login form on jsp pages

Login Form (index2.html)
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body bgcolor="96D2C2">
<form name="ask_1" method="get" action="index24.jsp">
Username: <input type="text" name="id11"/> <BR>
Password: <input type="password" name="id22" /> <BR>
<input type="submit" value="Login" /> <BR>
<BR>
</form>
Create an Account
</body>
My JSP code in order to verify the login info and print to user success or not... (index24.jsp)`
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.*" %>
<% int j=0; %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/mydb1?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
String id11=request.getParameter("id11");
String id22=request.getParameter("id22");
String sqlString = "SELECT FROM users WHERE username='"+id11+"' AND password='"+id22+"' ";
ResultSet rs = myStatement.executeQuery(sqlString);
if(rs.next()) {
System.out.println("Success"); }
else {
System.out.println("Failed");
}
%>
</body>
</html>
The code given above is giving me the error, "The requested resource is not available.". Any suggestions and improvements to my code or edit are appreciated.
the Connection myConnection = DriverManager.getConnection(myDatabase);
is kind wrong because the getConnection() will not handle the url given as parameter String try to do it like this :
Connection myConnection = DriverManager.getConnection("jdbc:mysql://localhost/mydb1" , "root" , "1234");
you can check the documentation of that method , and dont'forhet to remove this line
String myDatabase = "jdbc:mysql://localhost:3306/mydb1?user=root&password=1234";
Try to close the "try {" block with
} catch (Exception e) {
e.printStackTrace(new java.io.PrintWriter(out));
}
to get more details about the error. It is a simple trick for developing jsps and should be removed in an official version of a web-site.
Also, it may be out of topic, but your code is susceptible to SQL injection. See more here
http://www.w3schools.com/sql/sql_injection.asp
Finally the problem was the server (GlassFish)... i used Apache Tomcat and works properly...

Database connectivity in MySQL 5.5

I have written these 2 files:
register. html(which is for users for registering them) and register.jsp(for backend activity)
when the user registers he gets redirected to the server page but after redirection the page is blank show nothing..
Heres the code below for both the files:
`Register(html)
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="http://localhost:9090/RegTrial/sel.jsp">
Rec: <input type="text" name="rec">
<br>
Comp: <input type="text" name="comp">
<br>
<input type="submit" value="submit" name="submit">
<input type="reset" value="clear">
</form>
</body>
Register.jsp
<html>
<%#page import="java.sql.*" language="java"%>
<%#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>
<%
String rec_no=request.getParameter("rec");
String comp_no=request.getParameter("comp");
Connection con1;
Statement stmt1=null;
ResultSet rs;
String recDb=null,compDb=null;
try
{
out.println("Rec:"+rec_no);
out.println("Comp:"+comp_no);
out.println("*****************************");
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/register";
String user="root";
String password="department";
con1=DriverManager.getConnection(url,user,password);
stmt1=con1.createStatement();
String query="select receipt_no,comp_no from receipt where receipt_no = '"+rec_no+"' and comp_no = '"+comp_no+"'; ";
rs=stmt1.executeQuery("query");
if(rs.next())
{
recDb=rs.getString(1);
compDb=rs.getString(2);
out.println("Rec:"+rec_no);
out.println("RecD:"+recDb);
out.println("Comp:"+comp_no);
out.println("CompD:"+compDb);
if( rec_no.equalsIgnoreCase(recDb) && comp_no.equalsIgnoreCase(compDb))
{
out.println("already registered");
}
else
{
response.sendRedirect("www.google.com");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
I have set the environmental variables:
CATALINA_HOME: D:\apache-tomcat-7.0.65
classpath: .;D:\apache-tomcat-7.0.65\lib\servlet-api.jar;D:\apache-tomcat-7.0.65\jar\mysql-connector-java-5.1.37-bin.jar;
path: C:\Program Files (x86)\Java\jdk1.7.0_40\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.7.0_40
(Am using netbeans 7.3,mwsql 5.5,apache tomcat 7.0)

Access DB query not showing in Apache, here's the scriplet in Netbeans i have

This is what i have, when i run the jsp file on the browser the only thing i get is the "Report" header but not the query result, please guide me to the right way.
<%#page import="java.sql.ResultSet"%>
<%#page import="database.Dba"%>
<%#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>
<center> <h1> Report </h1></center>
<%
//scriplet
try{
Dba db =
new Dba(application.getRealPath("daw.mdb"));
db.conectar();
db.query.execute("select usuario from usuarios");
ResultSet rs = db.query.getResultSet();
while(rs.next()){
out.print(rs.getString(1)+ "<br>");
}
db.desconectar();
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>

Categories