User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 423,608 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,244 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 711 | Replies: 5
Reply
Join Date: Oct 2007
Posts: 3
Reputation: Rajesh.D.N is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Rajesh.D.N Rajesh.D.N is offline Offline
Newbie Poster

Whats Wrongin folloowind code for connecting with mysql

  #1  
Oct 15th, 2007
<html><body>

<%@ page import="java.sql.*" %>

<%

String user="root@localhost"; // root itself
String password="pwdl";
String connectionURL = "jdbc:mysql://localhost/Database_name";

Connection connection = null;

Statement statement = null;

ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, user, password);
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * table");

while (rs.next())
{
out.println(rs.getString("name")+"<br>"+rs.

getString("emailid")+"<br>"+rs.getString("contents

")+"<br>");
}

rs.close();

%>

</body></html>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,454
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 129
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Whats Wrongin folloowind code for connecting with mysql

  #2  
Oct 15th, 2007
1) This code should not be in a scriptlet. Move the query part out to a bean.

2) Use a connection pool, or open the connection in a session object (although this type of persistence is frowned upon) because opening a connection for every page load is extremely inefficient.

3) if you absolutely must use scriptlets (which you don't, but you may insist on it) then at least don't mix "html tag" strings into the middle of the scriptlet, that is just blurring the separation (and increasing the maintenance load) all that much more.

4) The user would generally be simply "root", as opposed to "root@localhost". The MySQL server will resolve the "@whatever" part itself, depending on where you connect from. Make sure you have the user@whatever login allowed over TCP in your MySQL configuration.

I think those are enough problems for now.
Last edited by masijade : Oct 15th, 2007 at 8:59 am. Reason: typo
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 3
Reputation: Rajesh.D.N is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Rajesh.D.N Rajesh.D.N is offline Offline
Newbie Poster

Re: Whats Wrongin folloowind code for connecting with mysql

  #3  
Oct 15th, 2007
No i dint get what u said.
I changed that root@localhost to root, i am getting ststus-500, internal error(). Plz can u sort out this problem
Reply With Quote  
Join Date: Feb 2006
Posts: 1,454
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 129
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Whats Wrongin folloowind code for connecting with mysql

  #4  
Oct 15th, 2007
The actual error message and stacktrace would help. The status 500 message simply tells you something went wrong, not what. Look in the logs for your application for the actuall error (not that I think it's going to help). If you didn't understand what I said, then you have a large learning curve in front of you, and you should read a couple of good tutorials before continuing.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Aug 2007
Posts: 74
Reputation: lookof2day is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Whats Wrongin folloowind code for connecting with mysql

  #5  
Oct 17th, 2007
String user="root@localhost"
should be
String user="root";
Reply With Quote  
Join Date: Feb 2006
Posts: 1,454
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 129
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Whats Wrongin folloowind code for connecting with mysql

  #6  
Oct 17th, 2007
He's already been told this, and then will only say that he gets a 500 status back, but has not provided an error message yet. I am willing to bet it is the mysql user configuration now.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 8:40 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC