• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connect my MySQl databse

 
nhlanhla mazibuko
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic