I. Generalization of project development
1, Classname,url,user, password common and unchanging values, defined as constants, written into a class (Constants)
Package mybean.util;
public class Constants {
public static String drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static String url= "jdbc:sqlserver://localhost:1433; Database=ebuy ";
public static String uid= "SA";
public static String pwd= "123456";
Database name, user name, password, URL is different
}
2. Create Jdbctool class, write connection database, close connection, realize reusability
Package mybean.util;
Import java.sql.*;
public class Jdbctool {
public static Connection getconnection ()
{
Connection Conn=null;
try {
Class.forName (Constants.drivername);
Conn=drivermanager.getconnection (CONSTANTS.URL,CONSTANTS.UID,CONSTANTS.PWD);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Return conn;
}
}
Development: by writing a common method, you can get connections to any database without modifying the source program
Solution: Put the relevant values into the configuration file, by modifying the configuration method implementation and the specific data decoupling
(1) Create a new file under the SRC folder, jdbc.properties
Classname=com.microsoft.sqlserver.jdbc.sqlserverdriver
Url=jdbc:sqlserver://10.40.43.202:1433;database=ebuy
User=shopping
Password=shopping
(2) New Jdbctool class under Mybean.util package
Package mybean.util;
Import java.util.*;
Import mybean.*;
Import java.sql.*;
import java.io.*;;
public class Jdbctool {
/*
* Write a common method to get a connection to any database without modifying the source program
*/
Public Connection getconnection () throws exception{
Connection Conn=null;
String Classname=null;
String Url=null;
String User=null;
String Password=null;
Read the jdbc.properties file input stream under the Classpath
InputStream In=getclass (). getClassLoader (). getResourceAsStream ("jdbc.properties");
Create a Properties Object
Properties Properties=new properties ();
Load in Object input stream
Properties.load (in);
/load String
Classname=properties.getproperty ("ClassName");
Url=properties.getproperty ("url");
User=properties.getproperty ("user");
Password=properties.getproperty ("password");
Properties Info=new properties ();
Info.put ("user", user);
Info.put ("password", password);
//Method 1
Driver driver= (Driver) class.forname (className). newinstance ();
Conn=driver.connect (URL, info);
//Method 2
Class.forName (className);//Load data-driven
Conn=drivermanager.getconnection (Url,user,password);
Return conn;
}
}
Second, the Commodity Management sub-page
What you need to know about paging is:
(1) A few pages countpage, the current page currentpage, the total number of records CountNumber, each page shows a few records pagenum
(2) need to get the data in the current page database Goodsdao.list (PageInfo)
1, the design of the page Bean--pageinfo.java
Package mybean.util;
public class PageInfo {
private int currentpage =1;//current page//2
private int pagenum = 4;//number of data per page//1
private int countnumber = 0; Total number of records 3
private int countpage=1;
public int Getpagenum () {
return pagenum;
}
public void Setpagenum (int pagenum) {
This.pagenum = Pagenum;
}
public int getcurrentpage () {
return currentpage;
}
public void Setcurrentpage (String currentpage) {
int page=1;
if (null! = CurrentPage && Currentpage.trim (). Length () >0)
{
Page=integer.parseint (currentpage);
}
else{
page=1;
}
this.currentpage = page;
}
public int Getcountnumber () {
return countnumber;
}
public void Setcountnumber (int countnumber) {
This.countnumber = CountNumber;
}
public void Setcountpage ()
{
This.countpage = (int) Java.lang.Math.ceil (This.getcountnumber ()/(This.getpagenum () +0.0));
}
public int Getcountpage ()
{
return countpage;
}
}
2, the database read the corresponding page data goodsdao.list (copy the original list code, appropriate changes)
Public linkedlist<goods> list (PageInfo PageInfo)
{
Linkedlist<goods> ls=new linkedlist<goods> ();
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String url= "Jdbc:sqlserver://localhost:1433;database=ebuy";
String user= "shopping";
String password= "shopping";
String sql= "select Top (1*) * from goods where GID is not in (select Top (1*) GID from goods)";
String classname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
try {
Class.forName (ClassName);
Conn=drivermanager.getconnection (URL, user, password);
First know how many records are in the database
String sql1= "SELECT COUNT (*) from goods";
Stmt=conn.preparestatement (SQL1);
Rs=stmt.executequery ();
if (Rs.next ()) {
Pageinfo.setcountnumber (Rs.getint (1));
}
Rs.close ();
Stmt.close ();
//Get a data record of the development bar
Stmt=conn.preparestatement (SQL);
Stmt.setint (1,pageinfo.getpagenum ());
Stmt.setint (2, Pageinfo.getpagenum () * (Pageinfo.getcurrentpage ()-1));
Rs=stmt.executequery ();
while (Rs.next ()) {
Goods g=new Goods ();
G.setgid (Rs.getint ("gid"));
G.setname (rs.getstring ("name"));
G.setprice (Rs.getfloat ("price"));
G.setnum (rs.getint ("num"));
Ls.add (g);
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
if (rs!=null)
{Rs.close ();}
if (stmt!=null)
{Stmt.close ();}
if (conn!=null)
{Conn.close ();}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return LS;
}
3, Good_view page to add some code implementation, display the specified page data
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "mybean.*,mybean.util.*"%>
<%
Display data for a specified page
String currentpage = Request.getparameter ("CurrentPage");
PageInfo pageinfo= (PageInfo) request.getattribute ("PageInfo");
if (pageinfo==null)
{
Pageinfo=new PageInfo ();
Request.setattribute ("PageInfo", pageInfo);
}
Pageinfo.setcurrentpage (currentpage); Set the current page
%>
<div style= "width:500px;" >
<p> Product Information Display </p>
<div style= "padding-left:350px;" ><a href= "good_add.jsp" > Add </a></div>
<table width= "border=" 1 ">
<tr>
<td> Commodity id</td>
<td> Product Name </td>
<td> Commodity price </td>
<td> Goods Quantity </td>
<td> Delete </td>
<td> Modify </td>
<td> Details </td>
</tr>
<%
Goodsdao dao=new Goodsdao ();
linkedlist<goods>gs=dao.list (pageInfo);
for (Goods G:gs)
{
Out.println (G.getname ());
%>
<tr>
<td><%=g.getgid ()%></td>
<td><%=g.getname ()%></td>
<td><%=g.getprice ()%></td>
<td><%=g.getnum ()%></td>
<td><a href= "Good_delete.jsp?gid=<%=g.getgid ()%>" > Delete </a></td>
<td><a href= "Good_update.jsp?gid=<%=g.getgid ()%>" > Modify </a></td>
<td> Details </td>
</tr>
<%}
Pageinfo.setcountpage (); Set Total pages
%>
</table>
<jsp:include page= "page.jsp"/>
</div>
4, PAGE.JSP implementation previous next page component
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<% @page import= "mybean.*"%>
<% @page import= "mybean.util.*"%>
<script type= "Text/javascript" >
function Pagechange (currentpage) {
document.getElementById (' CurrentPage '). Value = CurrentPage;
Document.form1.submit ();
}
</script>
<%
PageInfo pageinfo= (PageInfo) request.getattribute ("PageInfo");
if (pageinfo!=null)
{
int Countpage=pageinfo.getcountpage ();
int uppage=1;
int downpage=countpage;
if (Pageinfo.getcurrentpage ()!=1)
{
Uppage=pageinfo.getcurrentpage ()-1;
}
if (Pageinfo.getcurrentpage ()!=countpage) {
Downpage=pageinfo.getcurrentpage (+1);
}
%>
<ul style= "List-style:none" >
<li style= "border:1px solid #ccc; width:50px;float:left;margin:5px;" href= "#" ></a> home </li>
<li style= "border:1px solid #ccc; width:50px;float:left;margin:5px; onclick=" Pagechange (<%=upPage%>); " > Prev </li>
<li style= "border:1px solid #ccc; width:50px;float:left;margin:5px; onclick=" Pagechange (<%=downPage%>); " > Next </li>
<li style= "border:1px solid #ccc; width:50px;float:left;margin:5px; onclick=" Pagechange (<%= Pageinfo.getcountpage ()%>); " > Last </li>
</ul>
<%}%>
<form id= "Form1" Name= "Form1" action= "good_view.jsp" method= "post" target= "_self" >
<input type= "hidden" name= "CurrentPage" id= "CurrentPage" value= "<%=pageinfo.getcurrentpage ()%>" >
</form>
This article is from the "Learn Without thinking" blog, please be sure to keep this source http://dyzyxy.blog.51cto.com/944775/1625820
JSP Tenth class: JSP project Development Advanced Operations 1