| | |
Whats Wrongin folloowind code for connecting with mysql
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
<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>
<%@ 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>
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.
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 9: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
----------------------------------------------
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
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
----------------------------------------------
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
•
•
Join Date: Aug 2007
Posts: 80
Reputation:
Solved Threads: 10
JSP Syntax (Toggle Plain Text)
String user="root@localhost"
JSP Syntax (Toggle Plain Text)
String user="root";
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
----------------------------------------------
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
![]() |
Similar Threads
- Having an issue with connecting to MySQL (JSP)
- mysql DELETE not working (PHP)
- Problems connecting to MySQL (IT Professionals' Lounge)
- Help connecting to mysql tables (JSP)
- server notify client to update MySQL table (C)
- Problems using a php generator (PHP)
- can i use mysql database with asp (ASP)
- connecting to mysql in linux (Java)
Other Threads in the JSP Forum
- Previous Thread: how can i pass a value from java script to JSP scriplet
- Next Thread: open word document in java
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






