Servlet Connecting With Database

  • Uploaded by: seenu
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Servlet Connecting With Database as PDF for free.

More details

  • Words: 602
  • Pages: 9
Servlet connecting with database…. Stud deat: import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Com.Jeevan.Student.Information.*; import java.util.*; public class Stud_deat extends HttpServlet { public String name,cat,result,Sno; public int no,m1,m2,m3; public String a[]=new String[7]; //public String b[]=new String[3]; //public int c[]=new int[4]; public Stud_deat() { super(); } /** * Destruction of the servlet.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest req, HttpServletResponse res ) throws IOException ,ServletException { res.setContentType("Text/html"); PrintWriter out=res.getWriter(); HttpSession hs=req.getSession(); //String name=hs.getAttribute("username"); //if() String s="SEARCH";

if(s.equals(req.getParameter("add"))) { Sno= req.getParameter("l1"); Stud_Store ss=new Stud_Store(); ss.Retervie_Data(Sno); out.print("wwwwwwwwwwww"); out.print(ss.b[0]); out.print(ss.b[1]); out.print(ss.b[2]); out.print(ss.c[0]); out.print(ss.c[1]); out.print(ss.c[2]); RequestDispatcher rd=req.getRequestDispatcher("/Student_Details.jsp"); rd.include(req,res); //t6=form1.t6.value=ss.b[0]; /*out.print(aa.get(1)); out.print(aa.get(2)); out.print(aa.get(3)); out.print(aa.get(4)); out.print(aa.get(5)); out.print(aa.get(6));*/ } else { a[0]=req.getParameter("l1"); a[1]=req.getParameter("l2"); a[2]=req.getParameter("ll"); a[3]=req.getParameter("l3"); a[4]=req.getParameter("l4"); a[5]=req.getParameter("l5"); a[6]=req.getParameter("l6"); out.print(no+name+cat+m1+m2+m3+result); Stud_Store ss=new Stud_Store(); ss.Insert_Data(a); } } /** * Initialization of the servlet.
* * @throws ServletException if an error occure */ public void init() throws ServletException {

// Put your code here } }

******************************** Check user: import java.sql.DatabaseMetaData; import java.util.*; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.*; import sun.security.krb5.internal.a1; import Com.Jeevan.Student.Information.*; import java.io.IOException; import java.io.PrintWriter; public class Check_User extends HttpServlet { static String name; static String pwd; int i=0; /** * Constructor of the object. */ public Check_User() { super(); } /** * Destruction of the servlet.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html"); PrintWriter out = res.getWriter(); String name=req.getParameter("text1"); String pwd= req.getParameter("text2"); Stud_Info ss=new Stud_Info(); ss.Database_Con(name); String check=ss.q; //if(check != null ) // { if(pwd.equals(check)) { RequestDispatcher rd=req.getRequestDispatcher("/Student_Details.jsp"); rd.include(req, res); //out.print("password"+ss.q); HttpSession hs=req.getSession(); hs.setAttribute("username", "stud"); } else { RequestDispatcher rd=req.getRequestDispatcher("/login.jsp"); rd.include(req,res); out.println("username password are invalid"); } } /** * Initialization of the servlet.
* * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } } **************************************

Packages: Student information: package Com.Jeevan.Student.Information;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; public class Stud_Info { public String q; public void Database_Con(String n) { System.out.println("username"+n); try { Class .forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:studsql"); PreparedStatement st=con.prepareStatement("select password from user1 where username=? "); st.setString(1, n); //st.setString(2, n1); // b=st.execute(); ResultSet rs=st.executeQuery(); while(rs.next()) { q=rs.getString(1); } System.out.println("password"+q); //rs.close(); st.close(); con.close();

}catch(SQLException s1) { s1.printStackTrace(); }catch (ClassNotFoundException s2) { s2.printStackTrace(); } } public static void main(String arg[]) { Stud_Info s=new Stud_Info(); s.Database_Con("flei"); } }

********************************* Student store: package Com.Jeevan.Student.Information; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; public class Stud_Store { public String a[]=new String[6]; public String b[]=new String[3]; public int c[]=new int[4]; int x1,x2,x3,x4; public List al; //public void Insert_Data(int n,String n1,String n2,int n3,int n4,int n5,String n6) public void Insert_Data(String s[]) { try { Class .forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("Jdbc:Odbc:studsql"); //PreparedStatement st=con.prepareStatement("select * from Stud_Info"); PreparedStatement st=con.prepareStatement("insert into Stud_Info(stud_no,stud_name,stud_category,mark1,mark2,mark3,result) values(?,?,?,?,?,?,?)"); x1=Integer.parseInt(s[0]); st.setInt(1,x1); st.setString(2, s[1]); st.setString(3, s[2]); x2=Integer.parseInt(s[3]); st.setInt(4, x2); x3=Integer.parseInt(s[4]); st.setInt(5, x3); x4=Integer.parseInt(s[5]); st.setInt(6, x4); st.setString(7, s[6]); // b=st.execute(); int rs=st.executeUpdate(); /*while(rs.next()) { System.out.println(rs.getString(1)); }*/ System.out.println("nothing"+rs); //rs.close(); st.close(); con.close(); }catch(SQLException s1) { s1.printStackTrace(); }catch (ClassNotFoundException s2) { s2.printStackTrace(); } } public void Retervie_Data(String s) { try { Class .forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:studsql");

//PreparedStatement st=con.prepareStatement("select * from Stud_Info"); PreparedStatement st=con.prepareStatement("select * from Stud_Info where stud_no=?"); x1=Integer.parseInt(s); System.out.println("x1"+x1); st.setInt(1,x1); ResultSet rs=st.executeQuery(); while(rs.next()) { c[0]=(rs.getInt(1)); b[0]=(rs.getString(2)); b[1]=(rs.getString(3)); c[1]=(rs.getInt(4)); c[2]=(rs.getInt(5)); c[3]=(rs.getInt(6)); b[2]=(rs.getString(7)); /*al.add(rs.getInt(1)); al.add(rs.getString(2)); al.add(rs.getString(3)); al.add(rs.getInt(4)); al.add(rs.getInt(5)); al.add(rs.getInt(6)); al.add(rs.getString(7));*/ } System.out.println(c[3]); //System.out.println("nothing"+rs); //rs.close(); st.close(); con.close(); }catch(SQLException s1) { s1.printStackTrace(); }catch (ClassNotFoundException s2) { s2.printStackTrace(); } //return al; } public static void main(String arg[]) { Stud_Store s=new Stud_Store(); //s.Insert_Data();

//s.Insert_Data(101,"fleix","M.C.A",60,77,80,"pass"); s.Retervie_Data("3"); //System.out.println(al); } } ******************************

Related Documents

Servlet
May 2020 23
Servlet
May 2020 21
Servlet
November 2019 60

More Documents from "Daisy"

Java Collection Classes
November 2019 26
Jdbc
November 2019 31
Struts Tutorial
November 2019 16
Scjp 5 Mockup Test With Q&a
November 2019 22
Jsp Lab Project
November 2019 30