import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class postjdbc extends HttpServlet {
Connection con = null;
String err = "";
//connect Database
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("postgresql.Driver");
String url = "jdbc:postgresql:email";
String user = "postgres";
String pwd = "postgres";
con = DriverManager.getConnection(url,user,pwd);
}
catch(Exception e) {
err = err + " sql Connection err = " +e;
}
}
//Disconnect Database
public void distory()
{
try{
if(con != null) {
con.close();
}
}catch(Exception e) {
err = err + " sql DisConnection err = " +e;
}
}
//main
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = null;
String passwdid = null;
String query = null;
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("");
out.println("Postgre Servlet(JDBC)");
out.println("");
try {
Statement stmt =con.createStatement();
query = "select * from userid ";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
name = rs.getString("user_id");
passwdid = rs.getString("passwd_id");
out.println(name + passwdid);
}
rs.close();
stmt.close();
} catch (Exception e) {
err = err + " sql select err = " +e;
}
if(!(err.equals(""))){
out.println(err);
}
out.println("");
out.close();
}
}
|