• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

procedure to establish database connectivity

 
Abhijit Kangale
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


/* 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.
 
Abhijit Kangale
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your CLASSPATH include mysql connector jar?
 
Abhijit Kangale
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pushpak...

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

Thanks...
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic