我也是菜鸟。大家互相学习jsp文件
<%-- ItemList.jsp --%>
<%@ page info="lists all items in Librarian database"
errorPage="ErrorPage.jsp" %>
<jsp:useBean id="itemlist"
class="dataaccess.ItemListBean"
scope="request" /><%
int numRows;
int id;
%><HTML>
<HEAD>
<TITLE>Item List Output</TITLE>
</HEAD><BODY><P ALIGN="right"><A HREF="http://localhost:8080/ProJavaData/servlet/ItemEditSvlt?action=new">New Item</A></P><TABLE BORDER=1 CELLSPACING=2 CELLPADDING=2>
<TR VALIGN="top">
<TH COLSPAN=2>Action</A>
<TH>ID #</TH>
<TH>Title</TH>
<TH>Author</TH>
</TR>
<%
numRows = itemlist.getNumberOfItems();
for (int k = 0; k < numRows; k++) {
id=itemlist.getItemID(k);
%>
<TR VALIGN="top">
<TD><B><A HREF="http://localhost:8080/ProJavaData/servlet/ItemEditSvlt?action=edit&itemid=<%= id %>">Edit</A></B></TD>
<TD><B><A HREF="http://localhost:8080/ProJavaData/servlet/ItemEditSvlt?action=delete&itemid=<%= id %>">Delete</A></B></TD>
<TD><%= id %></TD>
<TD><%= itemlist.getItemTitle(k) %></TD>
<TD><%= itemlist.getItemAuthor(k) %></TD>
</TR>
<%
}
%>
</TABLE>
</BODY>
</HTML>
jbean:
/*
* ItemListBean.java
*/package dataaccess;import java.io.*;
import java.beans.*;public class ItemListBean implements Serializable { // member declarations
int[] itemID;
String[] itemTitle;
String[] itemAuthor; /** Default Constructor */
public ItemListBean () {} /** Parametized Constructor */
public ItemListBean (int[] ID,
String[] title,
String[] author) { if (ID.length == author.length &&
ID.length == title.length) {
this.itemID = ID;
this.itemTitle = title;
this.itemAuthor = author;
}
} public int[] getItemID() {
return this.itemID;
} public int getItemID(int idx) {
return this.itemID[idx];
} public void setItemID(int[] ID) {
this.itemID = ID;
} public void setItemID(int idx, int newID) {
this.itemID[idx] = newID;
} public String[] getItemTitle() {
return this.itemTitle;
} public String getItemTitle(int idx) {
return this.itemTitle[idx];
} public void setItemTitle(String[] title) {
this.itemTitle= title;
} public void setItemTitle(int idx, String title) {
this.itemTitle[idx] = title;
} public String[] getItemAuthor() {
return this.itemAuthor;
} public String getItemAuthor(int idx) {
return this.itemAuthor[idx];
} public void setItemAuthor(String[] author) {
this.itemAuthor= author;
} public void setItemAuthor(int idx, String author) {
this.itemAuthor[idx] = author;
} public int getNumberOfItems() {
if (this.itemID == null) {
return -1;
} else {
return this.itemID.length;
}
} public void setArrays(int arraySize) {
this.itemID = null;
this.itemAuthor = null;
this.itemTitle = null; if (arraySize > 0) {
this.itemID = new int[arraySize];
this.itemAuthor = new String[arraySize];
this.itemTitle = new String[arraySize];
}
}}数据库连接:
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String qry = "SELECT ITEM_ID, ITEM_TITLE, ITEM_AUTHOR FROM ITEM ORDER BY ITEM_TITLE";
ItemListBean itemlist = new ItemListBean();
int numItems = 0; try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:librarian2", "sa", ""); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(qry); numItems = (rs.last() ? rs.getRow() : 0); if (numItems > 0) {
rs.beforeFirst();
itemlist.setArrays(numItems);
for (int k = 0; k < numItems; k++) {
if (rs.next()) {
itemlist.setItemID(k, rs.getInt(1));
itemlist.setItemTitle(k, rs.getString(2));
itemlist.setItemAuthor(k, rs.getString(3));
}
}
} } catch(ClassNotFoundException cnfe) {
System.out.println("ItemListSvlt: Failed loading driver.");
System.out.println(cnfe.getMessage());
} catch(SQLException sqle) {
System.out.println("ItemListSvlt: SQL error.");
System.out.println(sqle.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {}
return itemlist;
}
}
<%-- ItemList.jsp --%>
<%@ page info="lists all items in Librarian database"
errorPage="ErrorPage.jsp" %>
<jsp:useBean id="itemlist"
class="dataaccess.ItemListBean"
scope="request" /><%
int numRows;
int id;
%><HTML>
<HEAD>
<TITLE>Item List Output</TITLE>
</HEAD><BODY><P ALIGN="right"><A HREF="http://localhost:8080/ProJavaData/servlet/ItemEditSvlt?action=new">New Item</A></P><TABLE BORDER=1 CELLSPACING=2 CELLPADDING=2>
<TR VALIGN="top">
<TH COLSPAN=2>Action</A>
<TH>ID #</TH>
<TH>Title</TH>
<TH>Author</TH>
</TR>
<%
numRows = itemlist.getNumberOfItems();
for (int k = 0; k < numRows; k++) {
id=itemlist.getItemID(k);
%>
<TR VALIGN="top">
<TD><B><A HREF="http://localhost:8080/ProJavaData/servlet/ItemEditSvlt?action=edit&itemid=<%= id %>">Edit</A></B></TD>
<TD><B><A HREF="http://localhost:8080/ProJavaData/servlet/ItemEditSvlt?action=delete&itemid=<%= id %>">Delete</A></B></TD>
<TD><%= id %></TD>
<TD><%= itemlist.getItemTitle(k) %></TD>
<TD><%= itemlist.getItemAuthor(k) %></TD>
</TR>
<%
}
%>
</TABLE>
</BODY>
</HTML>
jbean:
/*
* ItemListBean.java
*/package dataaccess;import java.io.*;
import java.beans.*;public class ItemListBean implements Serializable { // member declarations
int[] itemID;
String[] itemTitle;
String[] itemAuthor; /** Default Constructor */
public ItemListBean () {} /** Parametized Constructor */
public ItemListBean (int[] ID,
String[] title,
String[] author) { if (ID.length == author.length &&
ID.length == title.length) {
this.itemID = ID;
this.itemTitle = title;
this.itemAuthor = author;
}
} public int[] getItemID() {
return this.itemID;
} public int getItemID(int idx) {
return this.itemID[idx];
} public void setItemID(int[] ID) {
this.itemID = ID;
} public void setItemID(int idx, int newID) {
this.itemID[idx] = newID;
} public String[] getItemTitle() {
return this.itemTitle;
} public String getItemTitle(int idx) {
return this.itemTitle[idx];
} public void setItemTitle(String[] title) {
this.itemTitle= title;
} public void setItemTitle(int idx, String title) {
this.itemTitle[idx] = title;
} public String[] getItemAuthor() {
return this.itemAuthor;
} public String getItemAuthor(int idx) {
return this.itemAuthor[idx];
} public void setItemAuthor(String[] author) {
this.itemAuthor= author;
} public void setItemAuthor(int idx, String author) {
this.itemAuthor[idx] = author;
} public int getNumberOfItems() {
if (this.itemID == null) {
return -1;
} else {
return this.itemID.length;
}
} public void setArrays(int arraySize) {
this.itemID = null;
this.itemAuthor = null;
this.itemTitle = null; if (arraySize > 0) {
this.itemID = new int[arraySize];
this.itemAuthor = new String[arraySize];
this.itemTitle = new String[arraySize];
}
}}数据库连接:
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String qry = "SELECT ITEM_ID, ITEM_TITLE, ITEM_AUTHOR FROM ITEM ORDER BY ITEM_TITLE";
ItemListBean itemlist = new ItemListBean();
int numItems = 0; try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:librarian2", "sa", ""); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(qry); numItems = (rs.last() ? rs.getRow() : 0); if (numItems > 0) {
rs.beforeFirst();
itemlist.setArrays(numItems);
for (int k = 0; k < numItems; k++) {
if (rs.next()) {
itemlist.setItemID(k, rs.getInt(1));
itemlist.setItemTitle(k, rs.getString(2));
itemlist.setItemAuthor(k, rs.getString(3));
}
}
} } catch(ClassNotFoundException cnfe) {
System.out.println("ItemListSvlt: Failed loading driver.");
System.out.println(cnfe.getMessage());
} catch(SQLException sqle) {
System.out.println("ItemListSvlt: SQL error.");
System.out.println(sqle.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {}
return itemlist;
}
}
1、找一个工具:我是用jbuilder。
2、找一本书:我用的是java数据编程指南(前面讲的可以,后面的ejb讲的不好)个人观点。
3、学习java在jbuilder里面的各种相对应的技术是如何实现的。(比如jsp/servlet/javabean或其他的ejb),我想只要你入门了,应该会比较快的。
呵呵