Tuesday, 28 May 2013

JSP Database Connectivity


Process:-1)Install Appache tomcat7.0.
2)Install  mysql server 5.5 
3) put mysql connector.jar into environment varaible.
4) start the appache server.



Connect.jsp

<%@ page import="java.sql.*"%>
<html>
<head>
<title>JDBC Connectivity</title>
</head>

<body>
<h1>JDBC Connection example</h1>

<%
  String db = request.getParameter("data");
  String user = data; //
  try
 {
    java.sql.Connection con;
    Class.forName("com.mysql.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/"+db, imms, "admin");
    out.println (data+ "database opened.");
  }
  catch(SQLException e) {
    out.println("SQLException caught: " +e.getMessage());
  }
%>

</body>
</html>



try for another



example.jsp





<%@ page import="java.sql.*" %>  <%@ page import="java.io.*" %>  <%! Connection con=null; %>  <%! Statement st= null; %>  <%! ResultSet rs= null; %>  <html>  <head><title> Example</title></head>  <body>  <%  out.println("connection");  try {  Class.forName("com.mysql.jdbc.Driver").newInstance();  }
catch(ClassNotFoundException ce)
{
out.println(ce);
}  try{  con = DriverManager.getConnection("jdbc:mysql://localhost/db","admin", "admin");  st = con.createStatement();  rs = st.executeQuery("SELECT id, name,address FROM employee");  while(rs.next())
 {  String name = rs.getString(1);  int age = rs.getInt(2);  out.println(name + " " +age+"<br>");  } // end of whilee  rs.close();  st.close();  con.close();  }catch(SQLException exception){  out.println("<!--");  exception.printStackTrace(pw);  out.print(sw);  sw.close();  pw.close();  out.println("-->");  }  %>  </body>  </html> 

No comments:

Post a Comment