- main in mydb\Db1
- ===> index.jsp is old code
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mydb; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author user | |
*/ | |
public class Db1 { | |
Connection con = null ; | |
public void connect(){ | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
con = DriverManager.getConnection("jdbc:mysql://localhost/dbname", "root", "password"); | |
} catch (ClassNotFoundException ex) { | |
Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (SQLException ex) { | |
Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
public ArrayList<TUser> getUser(){ | |
ArrayList<TUser> lst = new ArrayList<TUser>(); | |
Statement stmt = null ; | |
try { | |
stmt = this.con.createStatement(); | |
ResultSet rs = stmt.executeQuery("select * from table_user limit 1 ,20 "); | |
while (rs.next()){ | |
TUser u = new TUser(rs); | |
lst.add(u); | |
} | |
} catch (SQLException ex) { | |
Logger.getLogger(Db1.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
return lst ; | |
} | |
public static void main(String[]args ){ | |
System.out.println("main test unit"); | |
Db1 db1 = new Db1(); | |
db1.connect(); | |
ArrayList<TUser> lst = db1.getUser(); | |
for(TUser user : lst){ | |
System.out.println(user.data1 + "\t" + user.fullname ); | |
} | |
System.out.println("job finished"); | |
} | |
} |
<%-- | |
Document : index | |
Created on : Apr 7, 2017, 2:42:15 PM | |
Author : user | |
--%> | |
<%@page contentType="text/html" pageEncoding="UTF-8"%> | |
<%@page import="mydb.Db1" %> | |
<%@page import="java.util.ArrayList" %> | |
<% | |
Db1 db1 = new Db1(); | |
db1.connect(); | |
ArrayList<String> lst = db1.getUser(); | |
pageContext.setAttribute("lst", lst); | |
%> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>JSP Page</title> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
<table boreder="1"> | |
<thead> | |
<tr><td>FullName </td></tr> | |
</thead> | |
<tbody> | |
<% lst = (ArrayList<String>)pageContext.getAttribute("lst"); %> | |
<% for(String fullname : lst ){ %> | |
<tr> | |
<!-- THIS IS OLD CODE ERRROR !!! --> | |
<td><% out.print(fullname); %></td> | |
</tr> | |
<% }%> | |
</tbody> | |
</table> | |
</body> | |
</html> |
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package mydb; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author user | |
*/ | |
public class TUser { | |
String fullname ; | |
int id ; | |
String data1 ; | |
public TUser(ResultSet rs ){ | |
try { | |
fullname = rs.getString("fullname"); | |
id = rs.getInt("id"); | |
data1 = rs.getString("data1"); | |
} catch (SQLException ex) { | |
Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |