Database connectivity in MySQL 5.5 - java

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)

Related

How can i get the data of the input hidden field whose name equals to a method and a string in java

Here is a sample jsp page with a form.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="h.abc" %>
<%
abc p = new abc();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="abcd.jsp" method="POST">
<input type="hidden" name="<%=p.getName()%>+'Name'" value='y'>
<input type='text' name='<%=p.getName()%>' >
<input type='submit' value='submit'>
</form>
</body>
</html>
Here is the abc class in the 'h' package.Please pardon the naming.It's only for illustration purposes.
public class abc {
public String name="abc";
public abc()
{
}
public String getName()
{
return name;
}
}
And this is the abcd.jsp target page.Here i am trying to get the value of the input field.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="h.abc" %>
<%
abc p = new abc();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String s = p.getName();
out.println(request.getParameter(s+"Name"));
out.println(request.getParameter(s));
%>
</body>
</html>
I am unable comprehend why the input field is not returning 'y' as its value.rather it is returning null.
change the code in first jsp
<input type="hidden" name="<%=p.getName()%>Name" value='y'>
you need put your abc instance in HttpServletRequest'attributes by calling setAttribute("you_key", abcInstance) method. And then you can get abc instance in your jsp pages by calling req.getAttribute("your_key") or using el expression

Unable to compile class for JSP error tomcat,mysql

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.

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...

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>

Error in uploading large size of files : EXC E:java.io.IOException: Posted content length of 12000169 exceeds limit of 1048576

I got this error when I try to upload 11.4 MB text file.
EXC E:java.io.IOException: Posted content length of 12000169 exceeds limit of 1048576
Here's my upload.jsp
<html>
<body >
<form action="read.jsp" method="post" ENCTYPE="multipart/form-data" >
<head>
<link rel="stylesheet" type="text/css" href="./css/master.css" />
<link rel="stylesheet" type="text/css" href="./css/nav.css" />
</head>
<p>
<h1>UPLOAD</h1><br>
</p>
<p>
<h3> Please specify a file:</h3><br>
<input type="file" name="file" size="40">
</p>
<div>
<input type="submit" value="Send">
</div>
</form>
</body>
</html>
Here's my read.jsp
<html>
<head>
<title>display</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%#page import="java.io.*" %>
<%#page import="com.oreilly.servlet.MultipartRequest" %>
</head>
<body >
<link rel="stylesheet" type="text/css" href="./css/master.css" />
<link rel="stylesheet" type="text/css" href="./css/nav.css" />
<p>
<h1> read text file</h1><br>
</p>
<%
String path="/apps/text/";
String fileName="";
String PhoneModel = GetPhoneModel();
File a1=null;
try{
MultipartRequest multi=new MultipartRequest(request, path);
a1=multi.getFile("file");
} catch (Exception e){out.print("EXC E:"+e);}
try{
BufferedReader is = new BufferedReader(new FileReader(a1));
String fileData = "";
String crossSell="";
while((fileData = is.readLine()) != null)
{
out.println(fileData
}
}catch(Exception e){}
%>
<p>
<h1>Process Completed!</h1><br>
</p>
</body>
</html>
How can I remove the maximum limit of content length so that I can upload large files?
MultipartRequest has a default max size of 1048576. you can change this when calling the constructor.
MultipartRequest multi=new MultipartRequest(request, path, newSize);

Categories