wood burning stoves*
The moose likes JDBC and the fly likes connecting to SQL database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "connecting to SQL database" Watch "connecting to SQL database" New topic
Author

connecting to SQL database

Niall Moynihan
Greenhorn

Joined: Apr 22, 2005
Posts: 15
Hello im trying to connect a mySql database to my website. I am using a Tomcat server and my connection code is in java. The server is up and running ok and i have my connection code which i believe is alright(see below) but it wont connect up for me. Once i run MY JSP page through the server ie. localhost 8080, the page runs alright but its as if not see my connection code at all. Its doesn’t say if its made a connection or not. Dose anyone have any ideas what the problem may be? Thanks
<html>

<head>
<script language="java">
import java.sql.*;
import java.io.*;
import java.sql.Connection; //MIGHT NEED TO COMMETED OUT
import java.sql.DriverManager; //MIGHT NEED TO COMMETED OUT
import java.sql.SQLException; //MIGHT NEED TO COMMETED OUT


class ConnectDB
{
public static void main (String[] args) throws IOException
{
Connection conn = null;

try
{
String userName = "root";
String password = "password";
String url = ("jdbc:mysql://127.0.0.1:3306/niall");

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection (url, userName, password);

System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
THIS IS A TEST
</body>
</html>
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

If this is the code, I dont think it would run.

Java is server side if you use in JSP,you have written all java code in side script tag ,and your code is being treated as script code

Please read something more about JSP

Shailesh
[ April 27, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Niall Moynihan
Greenhorn

Joined: Apr 22, 2005
Posts: 15
Im not quite sure what you mean. I cant get my head around what the problem is. I have been reading up on JSPs but i thought this code would work. Should i not have the connection code in the script?
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Niall Moynihan:
I have been reading up on JSPs but i thought this code would work


Did you see similar example in any book <script language="java"> is not the way to put java code in JSP

I can only say read on JSP,start from here
http://java.sun.com/products/jsp/html/jspbasics.fm.html

Shailesh
Niall Moynihan
Greenhorn

Joined: Apr 22, 2005
Posts: 15
il have a look at that link. Thanks for your help
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I would also recommend the following:
Java Server Pages Tutorial
Niall Moynihan
Greenhorn

Joined: Apr 22, 2005
Posts: 15
Hello, iv looked at the links you gave me and changed my code accordingly but i still cant get it to work. I have put my changed code below. Can anyone see what the problem might be? I dont know what else to do!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<%@ page import="java.util.*" %>


<%
//import java.sql.*;
//import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.SQLException;


class ConnectDB
{
public static void main (String[] args) throws IOException
{
Connection conn = null;

try
{
String userName = "root";
String password = "password";
String url = ("jdbc:mysql://127.0.0.1:3306/niall");

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection (url, userName, password);

System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
%>


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
THIS IS A TEST
</body>
</html>
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Basically, you have a created a regular POJO(plain old java object) and inserted it into to JSP page. Your code has little or no behavior other than connecting to a MySQL database.

1. I would recommend removing the code from the JSP page and placing it into a JavaBean.

2. Provide and/or create methods that will provide the user access to your database connection i.e. verify a logon/password, verify an entry exists in the database etc.

3. Access this JavaBean inside your JSP page.


There is a lot more involved to get all of the pieces to work together than what I mentioned above. So here is yet another link that may help you out:
JavaBeans component

I would also recommend downloading the fullJ2ee Tutorial

The tutorial comes with I believe full examples that may also answer some questions.
Niall Moynihan
Greenhorn

Joined: Apr 22, 2005
Posts: 15
Thanks again. Much appreciated!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connecting to SQL database
 
Similar Threads
driver cannot be found error
driver problem
unable to load mysql driver through servlet program
What is the problem with this code!!!
Connection with database Help