dcsimg
how to searching data from database in servlet??
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   mr._nash
Posted On:   Wednesday, December 21, 2011 03:11 PM

actually the coding below was succesfully to insert data to database. But i need to SEARCH back data from database..how to make it possible and where i must put the query?? import java.io.DataInputStream; import java.io.InputStream; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; import javax.swing.*; import java.sql.*; public class ConnectServletMobile extends HttpServlet { static String username="root", password=""; static String url = "jdbc:mysql://localhost/mobile"; static Statement stmt; static PreparedStatement pstmt; static C   More>>

actually the coding below was succesfully to insert data to database. But i need to SEARCH back data from database..how to make it possible and where i must put the query??


import java.io.DataInputStream;

import java.io.InputStream;

import javax.servlet.http.*;

import javax.servlet.*;

import java.io.*;

import javax.swing.*;

import java.sql.*;


public class ConnectServletMobile extends HttpServlet
{

static String username="root", password="";

static String url = "jdbc:mysql://localhost/mobile";

static Statement stmt;

static PreparedStatement pstmt;

static Connection conn;

static ResultSet rs;


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

doPost(request,response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{


System.out.println("3");

DataInputStream data = new DataInputStream((InputStream)request.getInputStream());

String message = "";

int no = data.readInt();

String date = data.readUTF();

String vNo = data.readUTF();

String sLoc = data.readUTF();

String sDesc = data.readUTF();

int Sno=data.readInt();



try
{

System.out.println("4");


Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("45");

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


System.out.println("5");

stmt = conn.createStatement();

System.out.println("56");

rs = stmt.executeQuery("SELECT * FROM summon WHERE s_no ="+ no);

System.out.println("6");


if(rs.next())
{

message = "The Summon was existed";

System.out.println("7");

}

else
{



pstmt = conn.prepareStatement("INSERT INTO summon VALUES(?,?,?,?,?,?)");

pstmt.setInt(1,no);

pstmt.setString(2,date);

pstmt.setString(3,vNo);

pstmt.setString(4,sLoc);

pstmt.setString(5,sDesc);

pstmt.setInt(6,Sno);

pstmt.executeUpdate();

System.out.println("8");

stmt.close();

pstmt.close();

conn.close();

message = "Summon were inserted";


}


response.setContentType("text/plain");

response.setContentLength(message.length());

PrintWriter out = response.getWriter();

out.println(message);

System.out.println("9");
}


catch(Exception ex)
{
message = ex.toString();

System.out.println("10 ex: "+message);
}


}


}    <<Less
About | Sitemap | Contact