aspose file tools*
The moose likes JDBC and the fly likes What is the problem with this code!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the problem with this code!!!" Watch "What is the problem with this code!!!" New topic
Author

What is the problem with this code!!!

campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
I am trying to connect to a database with a username and a password through Jsp but I am getting error:

I have a Login Page that calls the process page to ]connect to the database

process page:

<html>
<%@ page import = "java.sql.*, dbConnect.ConnectClass" page ="java"%>
<jsp:useBean id = "myCBean" class="dbConnect.ConnecClass" scope="session"/>

<%
String path = request.getContextPath();
String basePath = "http://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>

<base href="<%=basePath%>">
Accessing database please wait........ <br>

<% myCBean.DbInit(); %>
Database connection is initialized successfully! <hr>

<title></title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
</body>

<% myCBean.DbClose(); %>
Database connection is closed!

</html>

my class to connect and check password
package dbConnect;
import java.sql.*;

public class ConnectClass {

private String username = "";
private String password = "";

public ConnectClass() {
}

public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void DbInit() throws SQLException{

public boolean authenticate(String username2,String password2) {
String query = "select * from WIZUSUARIOS;";
String DbUserName = "";
String DbPassword = "";
String finalUser = "";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://servername:1433");
Statement stat = con.createStatement();
ResultSet rst = stat.executeQuery(query);
while(rst.next()){

DbUserName = rst.getString("UserName");
DbPassword = rst.getString("password");

if (username2.equals(DbUserName) && password2.equals(DbPassword)) {

break;
}
}
return true;
}//try
catch(Exception e){
e.printStackTrace();
return false;
}
}//authenticate

}//dbInit
}

I am getting an error like error on token "(" ";" in the public boolean authenticate(String username2,String password2)
and in the process page.

Can anyone help with this because Iam running out of time I want to connect to database with JSP with a username and password and be able to change the username and password.

Thank you very much
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Well I changed originally to this:

minha class is:

package dbConnect;
import java.sql.*;

public class ConnectClass {

private String username = "";
private String password = "";

public ConnectClass() {
}

public void setUsername(String username) {
this.username = username;
}

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

//public void DbInit() throws SQLException{

public boolean authenticate(String username2,String password2){
String query = "select * from WIZUSUARIOS;";
String DbUserName = "";
String DbPassword = "";
String finalUser = "";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://ANDROMEDA:1433");
Statement stat = con.createStatement();
ResultSet rst = stat.executeQuery(query);
while(rst.next()){

DbUserName = rst.getString("UserName");
DbPassword = rst.getString("password");

if (username2.equals(DbUserName) && password2.equals(DbPassword)) {

break;
}
}
return true;

}//try
catch(Exception e){
e.printStackTrace();
return false;
}
}//authenticate

//}//dbInit
}

my jsp to process page is:

<%
String path = request.getContextPath();
String basePath = "http://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>

<base href="<%=basePath%>">
Accessing database please wait........ <br>

Database connection is initialized successfully! <hr>

<title></title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
</body>

<%--<% myCBean.DbClose(); %> --%>
Database connection is closed!
</html>

my login calls the process page but doesnt connect to the database this message cames only :
Accessing database please wait........ <br>

Why I cant connect to the database it looks like the class is not being called from the process page How do I call the class and another thing DO I have to also set up the database in the web.xml file??

Can anyone help please??

Thanks
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Is anyone out there to help me??

I can connect to the database with a main methos but had no luck with JSP dont want to use Servlets because it is only for testing.

Can anyone help me please as I dont know anyone that is a java programmer here in Rio.

Thank you
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Can you cut/paste the exact error. And also I would suggest you use the tags to display your source. It is alot easier to follow.

But one error I noticed was in your declaration of the <useBean> tag ..




shouldn't that be dbConnect.ConnectClass instead of dbConnet.ConnecClass.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What is the problem with this code!!!

You mean aside from the fact that you accessing a Database from inside a JSP page and your code is VERY hard to read?

I started to try and debug, but it hurt my eyes. Take cj's advice. Edit your 2 posts and put your code between UBB Code Tags.

Then we might start trying to understand your problem.


GenRocket - Experts at Building Test Data
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Sorry!! New here to post codes ok!

I am trying to connect to a database with a username and a password with Jsp but I am getting error:

I have the usual Login Page which have in the action="process.jsp" that calls the process page to connect to the database.

process page:









my class to connect and check the password and username

[/CODE]










}

}


I am getting an error like error on token "(" ";" in the public boolean authenticate(String username2,String password2)
and in the process page.

Can anyone help with this because Iam running out of time I want to connect to database with JSP with a username and password and be able to change the username and password.

When I take this out of the code my web application runs but doesnt connect with the database, the login page calls only the process page. I dont want to use Servlet only JSP and the class but how do I call the class also Do I need to put the <login - atribute in the web.xml> ??? Can anyone help please






THANK YOU
I hope I did right this time
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Sorry Look the mess I did!

I will do a new post.

very sorry
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

campos, you can edit your posts if you don't like the way the formatting came out. Sometimes I have to edit 2 or 3 times before I'm happy with it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Sorry trying again, just asking to be patient with me please as I am new here.

I want to connect to the SQL database from a web based login page and I want to use only JSP to process the pages and A class to check the password and username to connect to the database. Also I want to be able to change to a new username and password after logging in.
Any suggestion or critics is welcome please. Need to sort out this by monday and Dont know how or cant see what is wrong.


my jsp to process page is:


When I dont use the myCBean in the jsp and the DbInit() method in the class I can run the application from tomcat with no problem but still cant connect to the database all that the login page does is to call for the process.jsp page and not the database.

When I use the bean + the DbInit method which is as a comment I cant compile the class saying that an error token "(" in :

public boolean authenticate(String username2,String password2){

and in the process page doesnt accept the following code :

<%@ page import = "java.sql.*, dbConnect.ConnectClass" page ="java"%>
<jsp:useBean id = "myCBean" class="dbConnect.ConnectClass" scope="session"/>

SO can anyone see what I am doping wrong

thank you
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Well, lets take one problem at a time. Before move into the jsp/servlet arena, lets make sure your object that connects the the database compiles okay and your able to connect to the database and are able to retrieve the desired results.

The reason, I believe you are unable to compile the dbConnect.ConnectClass.java class when you uncomment the DbInit() method is that you defined/implemented the authenticate(...) method inside the DbInit() method, and I could be wrong but I think that is a no-no. Move the authenticate method outside the DbInit() method.



Before you even place your business(bean) method in the jsp page, make sure your object has been successfuly compiled and tested.

Another suggestion, I don't know if this is exactly how your source is coded, but indentation is our friend, and it is some much more easier to read.

One more suggestion and I will quit making suggestions. Move all your bussiness logic(acquire connection, closing the connection) from the JSP page and place it in your business class i.e.
dbConnect.ConnectClass.java.

In my opinion, your JSP should only have the method from your bean i.e.


Your opening and closing of your database should be handled internally in your bean class.

Just my opinion.

I hope this helps.

Craig
batchu kishore
Greenhorn

Joined: Jun 01, 2004
Posts: 1
Hi i think you are doing mistake in your code

in this statement

String query = "select * from WIZUSUARIOS;";

in above you need to remove semicolon after select statement.

instead of above

you use this statement

String query = "select * from WIZUSUARIOS";

i removed one semicolon in select statement.

i hope then it will work out.

Thank You
Kishore Batchu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Despite insisting upon doing DB access inside a JSP (a serious error in my opinion), this is not a JSP question. I'm moving this along to the JDBC forum.
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Thanks batchu!

Miss out that!!

and to Bear I know it sounds not safe at all but I was just looking for something quick as it is only for testing anyway.
 
Don't get me started about those stupid light bulbs.
 
subject: What is the problem with this code!!!