jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes connect my MySQl databse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "connect my MySQl databse" Watch "connect my MySQl databse" New topic

connect my MySQl databse

nhlanhla mazibuko

Joined: Mar 28, 2008
Posts: 16

please you help me, i want to connect a jsp with the mysql databse
how do i go about doing that.

please help.
denis sorn
Ranch Hand

Joined: Apr 30, 2008
Posts: 33
Java Server Pages have nothing to do with mysql. You can google for 'jdbc mysql' and you'll find a lot of quick starter tutorials about how to get and use mysql connection from java code.

You can then use the same method to obtain data in your servlet.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
MySQL handbook section. Look for the link about connector/J. Probably worth running some basic SQL from the command-line interface to confirm MySQL is running correctly first.
Java™ Tutorials section.
nhlanhla mazibuko

Joined: Mar 28, 2008
Posts: 16
This my jsp page, that i want connect it with the database.
this is a code.
help me.

Connection conn = null;
ResultSet result = null;
Statement stt = null;

try {
Class c = Class.forName("sun.jdbc.knotiondb.JdbcOdbcDriver");
catch (Exception e) {
System.out.println("Error occurred " + e);
try {
conn = DriverManager.getConnection("knotiondbc:mysql://locahost:3306/knotiondb","nhla123","nhlapass");
catch (SQLException e) {
System.out.println("Error found " + e);
try {
stt = conn.createStatement();
result = stt.executeQuery("MySQL Query * localhost:3306/knotionbd");
catch (SQLException e) {
System.out.println("Error occurred " + e);
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33107

Note that it's not a good practice to mix Java/JDBC code and JSP. The JDBC should be in a servlet (or class called from a servlet) and then forward to a JSP. It is harder to develop, troubleshoot, maintain and reuse code in a JSP. Which wil negatively impact getting it working.

In any case, what error do you get when you try to run your code?

Also, you should only have one try/catch. If an exception is caught, it doesn't make sense to continue.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
@nhlanhla mazibuko
Is there any any specific reason for which you are not using JDBC Driver for MySQL. Here's a sample code below for connecting to MySQL using mysql Connector/J:

Thanks and Regards
I agree. Here's the link: http://aspose.com/file-tools
subject: connect my MySQl databse
It's not a secret anymore!