struts2连接oracle数据库并显示数据到页面
- 建表
create table cost2( id number(4) primary key, name varchar2(50), base_cost number(4,2), stauts char(1) ); 插入几行数据 insert into cost2 values (1,‘本月流量消费’,10.0,1); insert into cost2 values (2,‘本月流量消费’,12.0,1); insert into cost2 values (3,‘本月流量消费’,9.0,0); - 把jar包加载到maven库
打开cmd输入这行命令mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3.0 -Dpackaging=jar -Dfile=E:\tools\orac_\ojdbc6.jar(这路径以自己的为准) 3.打开IDEA工具创建maven项目 在pom.xml导入依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.28</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3.0</version>
</dependency>
web.xml中配置过滤器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在resources文件下新建struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="ghh" namespace="/data" extends="struts-default">
<action name="f1" class="Action.OAction" method="find">
<result name="success">/info.jsp</result>
</action>
</package>
</struts>
在resources文件下新建jdbc.properties文件这里存放连接数据库的几个值
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@localhost\:1521\:XE
username=system
password=hfghf
- 编写实现功能代码
项目目录结构如下 实体类Account
package pojo;
public class Account {
int id;
String name;
Double base_cost;
String stauts;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getBase_cost() {
return base_cost;
}
public void setBase_cost(Double base_cost) {
this.base_cost = base_cost;
}
public String getStauts() {
return stauts;
}
public void setStauts(String stauts) {
this.stauts = stauts;
}
}
工具类DBLink连接数据库的
package util;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class DBLink {
static String username;
static String password;
static String url;
static String driver;
static {
Properties p=new Properties();
try {
p.load(DBLink.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
username=p.getProperty("username");
password=p.getProperty("password");
url=p.getProperty("url");
driver=p.getProperty("driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getcon() throws SQLException {
Connection con = DriverManager.getConnection(url,username,password);
return con;
}
public static void main(String[] args) throws SQLException {
System.out.println(getcon());
}
}
Dao层
package DAO;
import pojo.Account;
import util.DBLink;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
public class Dao {
public static List<Account> getAll() throws SQLException {
Connection con= DBLink.getcon();
Statement st=con.createStatement();
String sql="select * from cost2";
ResultSet rs=st.executeQuery(sql);
List<Account> ls=new LinkedList<>();
while (rs.next()){
Account acc=new Account();
acc.setId(rs.getInt(1));
acc.setName(rs.getString(2));
acc.setBase_cost(rs.getDouble(3));
acc.setStauts(rs.getString(4));
ls.add(acc);
}
return ls;
}
public static void main(String[] args) throws SQLException {
System.out.println(getAll().get(1).getId()+" "+getAll().get(1).getName()+getAll().get(1).getBase_cost()+" "+getAll().get(1).getStauts());
}
}
Action控制层
package Action;
import DAO.Dao;
import com.opensymphony.xwork2.ActionSupport;
import pojo.Account;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class OAction extends ActionSupport {
List<Account> ls=new LinkedList<>();
public List<Account> getLs() {
return ls;
}
public void setLs(List<Account> ls) {
this.ls = ls;
}
public String find() throws SQLException {
ls= Dao.getAll();
return "success";
}
}
- 页面上显示数据info.jsp利用迭代器
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:iterator value="ls" var="a">
<s:property value="#a.id"/>
<s:property value="#a.name"/>
<s:property value="#a.base_cost"/>
<s:property value="#a.stauts"/><br>
</s:iterator>
</body>
</html>
- 代码基本全部放出来了,可能写的项目流程我描述的不是很好,请见谅,最主要是学习怎么用代码连接oracle数据库,然后将数据显示到页面来。
|