954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can anyone please help me with connect to database

sorry for annoying, i am quite stupid in it.

<html>
<head>
<title>Welcome to the online Auction...</title></head>
<body>
 
 
<% try
 
{
 
	String strUsername = request.getParameter("username"); 
	String strPassword = request.getParameter("password"); 
	
	Class.forName ("mysql-connector-java-5.0.8-bin"); 
 
 	Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/user.jsp,admin,admin");
 	
 	Statement myStatement = myConn.createStatement ();
 	
	String strSQL = "SELECT UserName,UserPwd from user where UserName="",UserPwd="";
	
 
	ResultSet myResult = myStatement.executeQuery(strSQL);
	
	String strUser = myResult.getString("UserName");
	String strPass = myResult.getString("Password");
	
	while(myResult.next())
    
    {
	
	if(strUsername.equals(strUser) && strPassword.equals(strPass))
	
                {
                    out.println("Login Successful!");
                }
                else
                {
                    out.println("Login Fail!");
                }
	
	}	
		
	myConn.close();
}
	catch(Exception e){} 
%>
			
</body> 
</html>


i am new to this.

Please help me with this.

my jar file name is mysql-connector-java-5.0.8-bin

Please help me out.

thanks

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

For once do not use JSP to connect to DB. JSP is for presentation and servlet is for logic. The order should be JSP collect data from user pass it on servlet. Servlet will validate data and if they correct it will attempt to connect to DB. If conection goes well DB operation is processed (insert/update/delete/chceck/retrieve) and servlet retrieve DB respond, after that servlet will either go to next page or return to previous

There is somewhere example how to do it that I posted about 2 years ago and it was recently re-open by somebody

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

can you show me some example for that...

i am too confuse with it

thanks

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

i saw ur code for the connection

public class SomeClassName extends HttpServlet
{
Connection conn;
Statement stmt;
	
public void init() throws ServletException
{
	try
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	}
	catch (Exception ex)
	{
		System.out.println("Exception is : " + ex.toString() );
	}
}
	
public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
	try
	{
		conn = DriverManager.getConnection("jdbc:mysql://localhost/processing.jsp"
			+ "visualogic?UserName=admin&UserPwd=admin");
		stmt = conn.createStatement();
	}
	catch(SQLException ex)
	{
		System.out.println("SQLException : " + ex.getMessage() );
	}public class SomeClassName extends HttpServlet
{
Connection conn;
Statement stmt;
	
public void init() throws ServletException
{
	try
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	}
	catch (Exception ex)
	{
		System.out.println("Exception is : " + ex.toString() );
	}
}
	
public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
	try
	{
		conn = DriverManager.getConnection("jdbc:mysql://localhost/processing.jsp" 
			+ "visualogic?UserName=admin&UserPwd=admin");
		stmt = conn.createStatement();
	}
	catch(SQLException ex)
	{
		System.out.println("SQLException : " + ex.getMessage() );
	}


am i doing right?
what file shall i save to?
.jsp or.....

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Above example is servlet so it should ba save as java class

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

i get error for it
here is the error

org.apache.jasper.JasperException: /processing.jsp(1,17) quote symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:200)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:150)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:162)
org.apache.jasper.compiler.ParserController.getPageEncodingForJspSyntax(ParserController.java:451)
org.apache.jasper.compiler.ParserController.determineSyntaxAndEncoding(ParserController.java:392)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:173)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:153)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:294)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:281)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Your connection string

conn = DriverManager.getConnection("jdbc:mysql://localhost/processing.jsp"
			+ "visualogic?UserName=admin&UserPwd=admin");

VS


my connection string

conn = DriverManager.getConnection("jdbc:mysql://localhost/" 						+ "database_name?user=my_username&password=my_password");

Can you explain me what is the purpose ofprocessing.jsp in your code?

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

it is trying to get to authencate the username and password.

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

The "processing.jsp" is not supposed to be there. Connection string uses the database URL
jdbc:mysql://localhost/ , database name database_name , database user name UserName=admin and password UserPwd=admin to establish connection with database.

Please read through these Java tutorials , just be aware they using Derby database instead of MySQL

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

hmm, not really understand it

sorry for annoying

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Well then tell us what you do not understand

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/"
			+ "visualogic UserName=admin&UserPwd=admin");
conn = DriverManager.getConnection("jdbc:mysql://localhost/" 						+ "database_name?user=my_username&password=my_password");		conn = DriverManager.getConnection("jdbc:mysql://localhost/" 						+ "database_name?user=my_username&password=my_password");


Behind the localhost/ what shall i put?
and the database_name=my database name?
my_username&password=my database username and password?

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This is correct

conn = DriverManager.getConnection("jdbc:mysql://localhost/"+ "visualogic UserName=admin&UserPwd=admin");

port number is only used when you connecting to IP address of server, however as DB is on local machine the keyword "localhost" is used. Secondly 8080 is port for Tomcat not for database

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

oh, let me try it again

thanks

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

another question, after i wrote this code

public class SomeClassName extends HttpServlet
{
Connection conn;
Statement stmt;
	
public void init() throws ServletException
{
	try
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	}
	catch (Exception ex)
	{
		System.out.println("Exception is : " + ex.toString() );
	}
}
	
public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
	try
	{
		conn = DriverManager.getConnection("jdbc:mysql://localhost:/"
			+ "visualogic UserName=admin&UserPwd=admin");
		stmt = conn.createStatement();
	}
	catch(SQLException ex)
	{
		System.out.println("SQLException : " + ex.getMessage() );
	}public class SomeClassName extends HttpServlet
{

what shall i do?

or can i use this code

<%@page import=java.util.*"%>
<jsp:useBean id="idhandler" class="authenticate.login"scope="request">
<jsp:setProperty name="idhandler" property="*"/>
</jsp:useBean>
<%
	if(idhandler.authenticate()){
%>
<jsp:forward page="details.jsp"/>
<% }
else{ %>
<jsp:forward page="error.jsp"/>
<% } %>
package authenticate;
import java.sql.*;
public class Login{
private String username="";
private String password="";
public Login(){
}
public void setUsername(String username){
this.username=username;
}

public void setPassword(String password){
this.password=password;
}

piblic String getUsername()
{
	return username;
}

public String getPassword()
{
	return password;
}

public boolean authenticate(){
String query="select * from user;";
String DbUserName;
String DbUserPwd;
try{
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:/"+ "visualogic UserName=admin&UserPwd=admin");
	Statement st=con.createStatement();
	ResultSet rs=st.executeQuery(query);

while(rs.next()){
DbUsername=rs.getString("username");
System.out.println("username:"+DbUserName);
System.out.println("Entered user name:"+username);
DbUserPwd=rs.getString("Password");
System.out.println("Password="+DbUserPwd);
System.out.println("Entered password:"+password);
if(username.equals(DbUserName) && password.equals(DbUserPwd))
{
	return true;
}
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
}
Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Use the first code, the second one is a bad practice.

To your question what should you do with first code. This code is general bridge to setup connection, before this you should collect some data from the form and now you would try to put them in DB or somebody just pressed one of the buttons that has functionality based on some sort of DB communication (get total sum for monthly income in shop for example). So JSP provide input and pass it on the servlet from which you have the opening part. Servlet does form validation if neccesary, proces or retrieve data from DB and provide appropriate re-direction to next JSP (usualy).

Read the tutorials which I gave you link and apply that to what you already got. Also you may want to look at this too

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

kid's still insisting on doing database access from JSP.
Maybe if it would stop trying that it would get somewhere...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Jwenting, thanks for the advise.
I get the second part from another site. :)

lets me try it again

thanks alot

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

let see this
user login page

<html>
<head>
<title>User Login Page</title>
<body>
<h2><center>Visualogic </center></h2>
<b>
<center><form name="form" method="post" action="????????">
	Username: <input type="text"name="username" value="">
	
	Password: <input type="password" name="password" value=""><p></p>
	
	<input type="submit" name="Submit" value="Login">
	
	<input type="reset" name="Reset" value="Reset"><p></p></center>
</form> 
</body>
</html>

connector.java

import java.sql.*;
public class SomeClassName extends HttpServlet
{
Connection conn;
Statement stmt;
	
public void init() throws ServletException
{
	try
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
	}
	catch (Exception ex)
	{
		System.out.println("Exception is : " + ex.toString() );
	}
}
	
public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
	try
	{
		conn = DriverManager.getConnection("jdbc:mysql://localhost:/"
			+ "visualogic UserName=admin&UserPwd=admin");
		stmt = conn.createStatement();
	}
	catch(SQLException ex)
	{
		System.out.println("SQLException : " + ex.getMessage() );
	}public class SomeClassName extends HttpServlet
{


for the form action, do i set it as connection.java so that when the user login , the data will validate it from the database?

Visualogic
Light Poster
29 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Action requere direction (file name) where to go next so it can look like this

action="http://localhost:8080/ProjectDirectoryName/servlet/NameOfServletWithoutExtension"


, there is advanced way how to work withaction element but for that you would have to know something about servlet mapping so leave it for now.
So now on submit button click you go to your servlet, that will initialize connection string, you should retrive values from JSP, validate them and then open connection to DB and do what ever you need. After that do not forget to close conection to DB and redirect process flow to appropriate page.
Note: Validation is part of your job, you have to design it.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You