Monday 24 December 2012

database connection with oracle thin driver in java


Hi friends today m vikram parihar going to tell you that how we can connect our oracle database with java program using oracle thin driver (class 12)....

"  if you are using netbeans  then you should copy or set the libraries with your class driver(oracle thin driver)
like this..

select the new project and add the libraries using project property.
Add jar/folder..


then ok and apply..

now set the ODBC services

steps for set the ODBC services
1.  Start -> Control Panel -> Administrative tools.
2. Data Sources (ODBC) -> now select the System DSN  like this..




  1. now we add the DSN  "Microsoft ODBC for Oracle" and  finish.


but first you must install Oracle database express or Oracle database enterprise edition.


now type the following code for fetch record form table.. (student table)

import java.sql.*;
import util.*;
class student
{
public static void main(String agrs[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
    }
        catch(ClassNotFoundException e)
                {
            system.out.println("not registered");
        }
try
{
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","vicky");

// here 127.0.0.1 is my local host IP address and 1521 is oracle database port number,  XE is my oracle //database and system is my username and vicky is my database password.

Statement st = con.createStatement();
 ResultSet rs = st.executeQuery("select name, fname, roll_no, class from student");
while(rs.next())
{
system.out.print(rs.getString(1));
system.out.print(rs.getString(2));
system.out.print(rs.getString(3));
system.out.print(rs.getString(4));
}

}
 catch(SQLException e)
            {
               system.out.println("not connected to database"+e);
            }
}
}

this code will work 100 % if not please connect to me..
thanks...
please give me feedback..  










Friday 14 December 2012

dynamic feedback page in jsp

hi friends m vikram parihar going to telling you that how we can add a feedback page in our dynamic website
and also we can store feedback in our database....  thanks..


first create a page name is feedback.jsp (use for view)

and write the code...



<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
                    <style>
#customers
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
width:71%;
margin-left:15%;
border-collapse:collapse;
}
#customers td, #customers th
{
font-size:1em;
border:1px solid #98bf21;
padding:3px 8px 2px 8px;
}
#customers th
{
font-size:1.1em;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#ffffff;
}
#customers tr.alt td
{
color:#000000;
background-color:#EAF2D3;
}
</style>
    <script>
            function mail()
            {
                var uid = document.getElementById("email").value;
            var atpos = uid.indexOf("@");
            var dotpos = uid.lastIndexOf(".");
            if(atpos<1 || dotpos<atpos+2 || dotpos+2>=uid.length || uid =="" || uid == null)
                {
                    alert("not a valid email address")
                    return false;
                }
            }
        </script>
    <div style="position:absolute;width:1px;height:800px;top:10px;left:207px;background-color:black"></div>
        <div style="position:absolute;width:1px;height:800px;top:10px;left:1152px;background-color:black"></div>
        <img style="position:absolute;top:100px;left:1200px;" src="image/door.gif">


        <%
        try
                {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            }
        catch(ClassNotFoundException e)
                               {
        out.print("not connected to database"+e);
        }
     
        try
                {
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","vicky");
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select name, message from userfeedback ");
            while(rs.next())
                {
                String name = rs.getString(1);
                String message = rs.getString(2);
     
               out.print("<table id= \"customers\">");%>
               <tr class =alt><td><%=name%></td><td><%=message%></td></tr>
                </table><%
}
            }
        catch(SQLException e)
                {
            out.print("sql query exception"+e);
        }
     
        %>
        <br>
        <%
        out.print("<form action = \"Handlefeedback.jsp\"  method = \"post\" onsubmit=\"return mail()\">");
        out.print("<table align = center>");
        out.print("<tr><td><u>Pass Comment</u></td></tr>");
        out.print("<tr><td>Name</td><td><input type = textbox name = \"name\" id = \"name\" size = 31>  </td></tr>");
        out.print("<tr><td>Email</td><td><input type = textbox name = \"email\" id = \"email\" size = 31>  </td></tr>");
        out.print("<tr><td>Message</td><td><textarea cols =25 rows =3  name = \"message\" id = \"message\"></textarea>  </td></tr>");
        out.print("<tr><td><button type = \"submit\">Post</button></td></tr>");
        out.print("</table>");
        out.print("</form>");
        %>





now write the code for handle the user feedback... n u can also store this feed back in  your database..
create jsp new page name is  feedbackHandle.jsp

and write the following code.. in this page


<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
    </head>
    <body>
        <%
        String name = request.getParameter("name").trim();
        String message = request.getParameter("message").trim();
        String email = request.getParameter("email").trim();
        out.print(name);
        out.print("<br>");
        out.print(message);
        out.print("<br>");
        out.print(email);


        try
                {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            }
        catch(ClassNotFoundException e)
                {
            out.print("not connect to database"+e);
        }
        %>
<%
        try
                {
             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","vicky");

            Statement stm = con.createStatement();
          stm.executeUpdate("insert into userfeedback values('"+email+"','"+name+"', '"+message+"')");
          }
       catch(SQLException e)
                {
            out.print("sorry database out of order"+e);
                }
     response.sendRedirect("feedback.jsp");
        %>
    </body>
</html>


at last when you will run your file u will see like this..