File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes connect my MySQl databse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "connect my MySQl databse" Watch "connect my MySQl databse" New topic
Author

connect my MySQl databse

nhlanhla mazibuko
Greenhorn

Joined: Mar 28, 2008
Posts: 16

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

please help.
nhlanhla
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
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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
Greenhorn

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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
 
 
subject: connect my MySQl databse
 
Similar Threads
connection problem with database after booting up linux
Help! DriverManager.getConnection problem
Help ! DriverManager.getConnection problem
Java MySQL connection
JdbcProgress connectivity