<%--
Document : SelectData
Created on : 4 Mar, 2021, 4:05:06 PM
Author : KANHA
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import = "java.lang.*"%>
<%@page import = "java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Select Data From Database</title>
</head>
<body>
<%
try{
//load the driver
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("oracle.jdbc.OracleDriver"); (oracle driver)
//establish the connection with database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
//con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","mca", "mca"); (oracle connection)
//sending sql query to database for execution
Statement st=con.createStatement();
//mysql query
String q = "select * from user";
//execute the query
ResultSet rs=st.executeQuery(q);
//for update,insert and delete use int i use for if else not mandatory
//int i =st.executeUpdate(q);
//fetch all data like php (while($row = mysqli_fetch_array($result));
while(rs.next()){
out.println(rs.getInt("id")+" "+rs.getString("name")+"<br>");
}
//used to close the connection
con.close();
}
//catch all the error inside try block
catch(Exception e){
out.println(e);
}
%>
</body>
</html>
1 Comments
Nice article
ReplyDelete