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 procedure to establish database connectivity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "procedure to establish database connectivity" Watch "procedure to establish database connectivity" New topic
Author

procedure to establish database connectivity

Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
Hiii all,

Can anyone tell me how can I establish database connectivity with MYSQL on Linux machine using JDBC driver. If possible give relevent code.

Regards,
Abhijit
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Abhijit Kangale wrote:Can anyone tell me how can I establish database connectivity with MYSQL on Linux machine using JDBC driver.

Show us what you have got so far. Tell us where you are stuck and we will try to point you in the right direction.
Abhijit Kangale wrote:If possible give relevent code.

We don't hand out ready made solutions here. Read this and this.
Sharad Jadhav
Greenhorn

Joined: Feb 10, 2010
Posts: 18


/* A servlet to display the contents of the Mysql Bedrock database */

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowBedrock extends HttpServlet
{
public String getServletInfo()
{
return "Servlet connects to Mysql database and displays result of a SELECT";
}

private Connection dbcon; // Connection for scope of ShowBedrock

// "init" sets up a database connection
public void init(ServletConfig config) throws ServletException
{
String loginUser = "postgres";
String loginPasswd = "supersecret";
String loginUrl = "jdbc:mysql://localhost/bedrock";

// Load the MySQL driver
try
{
Class.forName("com.mysql.jdbc.driver");
dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
}
catch (ClassNotFoundException ex)
{
System.err.println("ClassNotFoundException: " + ex.getMessage());
throw new ServletException("Class not found Error");
}
catch (SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
}

// Use http GET

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html"); // Response mime type

// Output stream to STDOUT
PrintWriter out = response.getWriter();

out.println("<HTML><Head><Title>Bedrock</Title></Head>");
out.println("<Body><H1>Bedrock</H1>");

try
{
// Declare our statement
Statement statement = dbcon.createStatement();

String query = "SELECT name, dept, ";
query += " jobtitle ";
query += "FROM employee ";

// Perform the query
ResultSet rs = statement.executeQuery(query);

out.println("<table border>");

// Iterate through each row of rs
while (rs.next())
{
String m_name = rs.getString("name");
String m_dept = rs.getString("dept");
String m_jobtitle = rs.getString("jobtitle");
out.println("<tr>" +
"<td>" + m_name + "</td>" +
"<td>" + m_dept + "</td>" +
"<td>" + m_jobtitle + "</td>" +
"</tr>");
}

out.println("</table></body></html>");
statement.close();
}
catch(Exception ex)
{
out.println("<HTML>" +
"<Head><Title>" +
"Bedrock: Error" +
"</Title></Head>\n<Body>" +
"<P>SQL error in doGet: " +
ex.getMessage() + "</P></Body></HTML>");
return;
}
out.close();
}
}

Hope this will help you.


Java J2EE Hibernate Spring
Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
Actually I am developing some application on Linux machine and written following code where I need to establish database connection using mysql driver. My task is to fethch data from database and put in textarea.

------------PROGRAM--------------------------------------------------------------------------------------------------

<%@ page import="java.sql.*" %>
<%@ page language="java" %>
<html>
<head>
<title> Doctor's Work Bench </title>
</head>
<body>
<center><h1> Welcome to Immunization Page </h1><br><br>
<%
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select IMMUNISATION_NAME from IMMUNISATION_REF_T");
while(rs.next())
{
String str=rs.getString("IMMUNISATION_NAME");
%>
<textarea rows="25" cols="20"> <%=str%> </textarea></center>
<% }
}
catch(Exception e)
{
out.println("Some Error has occoured"+e);
e.printStackTrace();
}


%>
</body>
</html>

-------------------------------------- END PROGRAM--------------------------------------------------------------------------------------------------

but when I execute the same in the browser it is giving following exception in the browser==>
ClassNotFoundException: com.mysql.jdbc.Driver

How can I slove this problem.

Thanks & Regards,
Abhijit
Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
hiii Vijay,

I got yout code but I need to do it using JSP. Please go through my code ( I have posted in reply ) and tell me where I m comitting mistake.

Regards,
Abhijit
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Does your CLASSPATH include mysql connector jar?
Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
I think this wherer I have strucked. I m not getting how to include jar on CLASSPATH. Where can I find this Mysql connector jar and how can proceed then. Since I m new to Linux I m not getting how to do this. Could you please explain me the same.

Thanks,
Abhijit
Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
Hi Pushpak...

Could you please tell me the procedure. I m trying but getting confused. please help.

Thanks...
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

Download the MySQL JDBC driver from the MySQL web site (or if you are building using Maven, you can let Maven download it for you) and add it to the "-cp" parameter on your command line. For example, assume the driver is in the current directory, and that my classes are in the target/classes directory, and my main class is org.peter.Main, then I would use:

java -cp target/classes:mysql-connector-java-5.1.6.jar org.peter.Main

Oh, wait, you are attempting to access the database from a servlet. In that case, place the mysql-connector-java-5.1.6.jar in the library directory of the servlet container or application server you are using. What app server are you using? If we knew that we could offer more specific advice.


JBoss In Action
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: procedure to establish database connectivity
 
Similar Threads
WAS and DB2 connectivity details
Example on struts database connectivity
JSP & JDBC implementation question
Database Connection Error
Apache Tomcat connectivity