| | |
can some one help me
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
-1
#2 Oct 14th, 2009
Netbeans.org - Creating a Simple Web Application Using a MySQL Database
PS: Do not limit your self to selected tools, try others or try also tutorials that are not directly fitting your tool set and modify as need it.
PS: Do not limit your self to selected tools, try others or try also tutorials that are not directly fitting your tool set and modify as need it.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 Oct 15th, 2009
Use a JDBC for connection and call this when connecting to the database. USe a servlet to take the details from the JSP then validate by querying the database by using some check class i.e. Boolean checkUser(username, password).
Return true if they exist and false if they do not. Here is an example JDBC -
Return true if they exist and false if they do not. Here is an example JDBC -
JSP Syntax (Toggle Plain Text)
package DatabaseConnection; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCConnector { String dbDriver = "your_db_connection"; String userId="db_userId"; // individual user id String password="db_password"; // individual password String dbURL = "db_location"+userId+"&password="+password; private Connection dbCon; private DatabaseMetaData dbMetaData; private Statement stmt; public JDBCConnector() { super(); } public boolean connect() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException { Class.forName(this.getDbDriver()).newInstance(); dbCon = DriverManager.getConnection(this.getDbURL()); return true; } public void close() throws SQLException { dbCon.close(); } public void commit() throws SQLException { dbCon.commit(); } public void setAutoCommit(boolean autocommit) throws SQLException { dbCon.setAutoCommit(autocommit); } public void rollback() throws SQLException { dbCon.rollback(); } public PreparedStatement prepareStatement(String sql) throws SQLException { PreparedStatement s = dbCon.prepareStatement(sql); return s; } public Statement createStatement() throws SQLException { Statement s = dbCon.createStatement(); return s; } public int executeUpdate(String s) throws SQLException { int count = stmt.executeUpdate(s); return count; } public ResultSet execSQL(StringBuffer sqlBuf) throws SQLException { Statement s = dbCon.createStatement(); String sql = sqlBuf.toString(); ResultSet rs = s.executeQuery(sql); return (rs == null) ? null : rs; } public String getDbDriver() { return this.dbDriver; } public void setDbDriver(String newValue) { this.dbDriver = newValue; } public String getDbURL() { return this.dbURL; } public void setDbURL(String newValue) { this.dbURL = newValue; } public ResultSet doQuery(StringBuffer qry) { Connection con = null; Statement stmt = null; // set variables to take in resultset values String returnedUserName = null; String returnedPassword = null; ResultSet rs = null; try { connect(); rs = execSQL(qry); } catch (ClassNotFoundException e) { System.out.println("ClassNotFound: " + e.getMessage()); } catch (SQLException e) { System.out.println("SQLException: " + e.getMessage()); } catch (InstantiationException e) { System.out.println("InstantiationException: " + e.getMessage()); } catch (IllegalAccessException e) { System.out.println("IllegalAccessException: " + e.getMessage()); } return rs; } }
0
#4 Oct 15th, 2009
To pardon_garden
First of all, this:
And also this is very "not inteligent":
It is exactly the same as
Also you do this:
The same goes for the "doQuery" method where you define 2 unnecessary variables even though you have global as well.
Peter_budo has created this amazing tutorial:
JSP database connectivity according to Model View Controller (MVC) Model 2
Why did you post this amateur code?
JSP Syntax (Toggle Plain Text)
ResultSet rs = s.executeQuery(sql); return (rs == null) ? null : rs;
First of all, this:
ResultSet rs = s.executeQuery(sql); never returns nullAnd also this is very "not inteligent":
return (rs == null) ? null : rs; If it is null return null, if it is not null return itself???????It is exactly the same as
return rs; Also you do this:
Statement s = dbCon.createStatement(); but I don't see where this instance can be closed?The same goes for the "doQuery" method where you define 2 unnecessary variables even though you have global as well.
Peter_budo has created this amazing tutorial:
JSP database connectivity according to Model View Controller (MVC) Model 2
Why did you post this amateur code?
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#5 Oct 15th, 2009
Because my amateur code still works.
By using the command db.close() the following method is invoked
and the connection is closed.
By using the command db.close() the following method is invoked
JSP Syntax (Toggle Plain Text)
public void close() throws SQLException { dbCon.close(); }
and the connection is closed.
0
#6 Oct 15th, 2009
•
•
•
•
Because my amateur code still works.
By using the command db.close() the following method is invoked
JSP Syntax (Toggle Plain Text)
public void close() throws SQLException { dbCon.close(); }
and the connection is closed.
Also you have a Statement declared globally and inside the method I described you open one locally, and you don't have methods to close neither of them.
Also you do this:
JSP Syntax (Toggle Plain Text)
public int executeUpdate(String s) throws SQLException { int count = stmt.executeUpdate(s); return count; }
Last edited by javaAddict; Oct 15th, 2009 at 9:33 am.
Check out my New Bike at my Public Profile at the "About Me" tab
0
#7 Oct 15th, 2009
•
•
•
•
I want to create a login jsp form that connect with servlet and mysql for database. And I'm using netbeans and glassfishV2. Please help me to solve this problem.
The link that I provided should be able to help you start. Also try Peter_budo's link
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Other Threads in the JSP Forum
- Previous Thread: Set Main Content to a Uniform Size
- Next Thread: inserting multiple selection from checkbox in to one column
Views: 438 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for JSP
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient project read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






