程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 簡單的struts應用開發(2)

簡單的struts應用開發(2)

編輯:關於JAVA

第二步: 配置數據庫

1.首先創建數據聯接bean, 並打包到 ConnectionPool, 它將從我們配置的聯接池中取得可用聯接.

// Java Document
package ConnectionPool;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class getDbConnection {
Connection conn;
Statement stmt;
ResultSet rs=null;
String foo = "Not Connected";
int bar = -1;
public getDbConnection() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/JDBC/TestDB");
if (ds != null) {
conn = ds.getConnection();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public Connection getCon()
{
return conn;
}
public void d_close() throws SQLException
{
if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}
}
protected void finalize() throws Throwable
{
try { d_close(); } catch (SQLException e) { ; }
}
}

2.創建數據庫.

數據庫名: mystruts

部門表: mydep

_______________________________________

dep_id dep_name

_______________________________________

1 dep1

2 dep2

3 dep3

4 dep4

_______________________________________

用戶表: myuser

______________________________________________

user_id user_name dep tel

______________________________________________

1 jack 1 0769-2454042

2 max 2 0769-2454043

______________________________________________

(注: 以上字段為了測試方便. 全部使用字符型)

第三步: 創建ActionForm (userActionForm.java)

package mystruts; //請打包到mystruts下.
import org.apache.struts.action.*;
import javax.Servlet.http.*;
public class userActionForm extends ActionForm {
private String action="add";
private String dep;
private String tel;
private String user_id;
private String user_name;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getDep() {
return dep;
}
public void setDep(String dep) {
this.dep = dep;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved