index.jsp
<%--Document : index
Created on : Oct 3, 2015, 11:17:49 AM
Author : Aravind Sankaran Nair
--%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body><br><br><br><br><br><br>
<center>
<table cellspacing="0" cellpadding="0" border="0" width="30%">
<tr bgcolor="#1E90FF">
<th>First Name</th>
<th>Last Name</th>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","root");
Statement st=con.createStatement();
ResultSet rs = st.executeQuery("select * from user"); // this is for name
while(rs.next())
{ int rowCount=rs.getRow();
if(rowCount%2==0){ %>
<tr bgcolor="#808080">
<td><%=rs.getString("firstname")%></td>
<td><%=rs.getString("lastname")%></td>
</tr>
<%}else {%>
<tr bgcolor="#D3D3D3">
<td><%=rs.getString("firstname")%></td>
<td><%=rs.getString("lastname")%></td>
</tr>
<% }%>
<% }%>
<%
rs.close();
st.close();
con.close();
%>
</table>
</center>
</body>
</html>
Database
1. create database testdb;
2. use testdb;
3. CREATE TABLE user (
`uid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NOT NULL,
PRIMARY KEY (`uid`)
);
4. insert into user values(1,'aravind','sankaran');
5. insert into user values(2,'jayesh','babu');
6. insert into user values(3,'lida','john');
7. insert into user values(4,'bento','rajan');
jar file needed:
mysql-connector-java-5.1.18-bin.jar
No comments:
Post a Comment