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
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Above example is servlet so it should ba save as java class
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Well then tell us what you do not understand
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
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
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902