JavaEE 實例 圖書管理系統


軟件151    馬金忠

一、打開數據庫,成功連接數據庫,並導入數據庫數據,很簡單 ,就不一一進行解釋了。。。

 

二、編寫代碼,編寫主界面的代碼和跳轉頁面等頁面的代碼,並調試運行成功:

  主要的包和文件如下圖:

  其主要界面的部分代碼: 

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<style type="text/css">
.style1{height:13px; width:67px;font-size:9px; align:center}
.style2{height:13px; width:62px;font-size:9px;}
</style>
</head>
<body bgcolor="#ffffff">
<table border="0" cellpadding="0" cellspacing="0" width="802">
<!-- 從Session中取出login -->
<s:set name="login" value="#session.login"></s:set>
<tr>
<td><img src="images/left.jpg" width="263" height="108" /></td>
<td><table align="left" border="0" cellpadding="0" cellspacing="0" width="539">
<tr>
<td><table align="left" border="0" cellpadding="0" cellspacing="0" width="539">
<tr><td><img src="images/title.jpg" width="310" height="72" /></td>
<td><table align="left" border="0" cellpadding="0" cellspacing="0" width="229">
<tr>
<td><img src="images/head.jpg" width="229" height="28"/></td>
</tr>
<tr>
<td><table align="left" border="0" cellpadding="0" cellspacing="0" width="229">
<!-- 如果login沒值,表示未登錄 -->
<s:if test="#login==null">
<s:form theme="simple" action="login" method="post">
<tr><td><input name="login.name" type="text" class="style1"/></td>
<td><img src="images/password.jpg" width="42" height="15"/></td>
<td><input name="login.password" type="password" class="style2"/></td>
<td><input type="image" src="images/login.jpg" style="width:57;height=15"></td>
</tr>
</s:form></s:if>
<!-- 否則表示登錄,登錄后就為輸入設置disabled,不可編輯 -->
<s:else><s:form theme="simple" action="login" method="post">
<tr><td><input name="login.name" type="text" class="style1" disabled/></td>
<td><img src="images/password.jpg" width="42" height="15"/></td>
<td><input name="login.password" type="password" class="style2" disabled/></td>
<td><input type="image" src="images/login.jpg" style="width:57;height=15" disabled></td>
</tr>
</s:form></s:else>
</table></td></tr>
<tr><td><img src="images/middle.jpg" width="229" height="29"/></td>
</tr></table></td>
</tr></table></td></tr>
<tr>
<td><table align="left" border="0" cellpadding="0" cellspacing="0" width="539">
<!-- 如果已經登錄,而且角色為管理員,true表示管理員,false表示學生
本例只做了2個功能,借書,和圖書管理,如果要編寫其他功能,只需在對應的圖片后面
加入點擊的觸發事件本部分調用的圖片里面的文字時發亮的,所以在頁面的視覺效果為亮
的-->
<s:if test="(#login!=null)&&(#login.role==true)">
<tr>
<td><img src="images/booksearch.jpg" width="76" height="36" /></td>
<!-- 點擊觸發操作,style="cursor:hand"表示當鼠標移動到該圖片是,
圖標變成手狀,下同 -->
<td><img src="images/lendsearch.jpg" width="74" height="36" /></td>
<td><img src="images/lend.jpg" width="72" height="36" onclick="location.href='lend.jsp'" style="cursor:hand"/></td>
<td><img src="images/giveback.jpg" width="71" height="36" /></td>
<td><img src="images/studentmanage.jpg" width="73" height="36" /></td>
<td><img src="images/bookmanage.jpg" width="71" height="36" onclick="location.href='bookmanage.jsp'" style="cursor:hand"/></td>
<td><img src="images/about.jpg" width="102" height="36" /></td>
</tr>
</s:if>
<!-- 如果已經登錄,而且角色為學生 ,后面功能的圖片中的文字是灰的,所以頁面效果功能是灰色的-->
<s:elseif test="(#login!=null)&&(#login.role==false)">
<tr>
<td><img src="images/booksearch.jpg" width="76" height="36" /></td>
<td><img src="images/lendsearch.jpg" width="74" height="36" /></td>
<td><img src="images/lend1.jpg" width="72" height="36" /></td>
<td><img src="images/giveback1.jpg" width="71" height="36" /></td>
<td><img src="images/studentmanage1.jpg" width="73" height="36" /></td>
<td><img src="images/bookmanage1.jpg" width="71" height="36" /></td>
<td><img src="images/about.jpg" width="102" height="36" /></td>
</tr></s:elseif>
<!-- 若前面兩種否就表示未登錄,本部分調用的圖片均為灰色的 -->
<s:else><tr>
<td><img src="images/booksearch1.jpg" width="76" height="36" /></td>
<td><img src="images/lendsearch1.jpg" width="74" height="36" /></td>
<td><img src="images/lend1.jpg" width="72" height="36" /></td>
<td><img src="images/giveback1.jpg" width="71" height="36" /></td>
<td><img src="images/studentmanage1.jpg" width="73" height="36" /></td>
<td><img src="images/bookmanage1.jpg" width="71" height="36" /></td>
<td><img src="images/about.jpg" width="102" height="36" /></td>
</tr></s:else></table></td>
</tr></table></td>
</tr></table>
</body>
</html>

圖片如下:

最主要的是連接數據庫成功!

代碼如下:

package db;
import java.sql.*;
public class DBConn {
public static Connection conn;
//連接數據庫
public static Connection getConn(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123");
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//關閉連接
public static void CloseConn(){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

成功后,點擊運行。。

當然運行的關鍵還是在pom.xml的配置上 :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cqive</groupId>
<artifactId>add</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging><!-- 必須設置為war,tomcat7才能加載,並且該項目必須配置web.xml -->

<repositories><!--設置maven組件倉庫 -->
<!-- maven官方倉庫 -->
<repository>
<id>maven</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- 阿里巴巴發布版本倉庫 -->
<repository>
<id>alibaba-opensource</id>
<name>alibaba-opensource</name>
<url>http://code.alibabatech.com/mvn/releases/</url>
<layout>default</layout>
</repository>
</repositories>

<build><!-- maven項目編譯插件 -->
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<!-- <encoding>UTF-8</encoding> -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>

<configuration>
<!-- 使用內置的模擬Tomcat服務器 -->
<path>/book</path>
<uriEncoding>UTF-8</uriEncoding>
<port>9627</port>
<mode>context</mode>
<!-- <contextFile>src/main/webapp/META-INF/context.xml</contextFile> -->

<contextReloadable>true</contextReloadable>
<!-- <backgroundProcessorDelay>5</backgroundProcessorDelay> -->
</configuration>
</plugin>
</plugins>
</build>

<dependencies><!-- 項目依賴插件 -->

<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope> <!-- 編譯時用到servlet-api和jsp-api,但在打包的時候不用這兩個依賴 -->
</dependency>

<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope> <!-- 編譯時用到servlet-api和jsp-api,但在打包的時候不用這兩個依賴 -->
</dependency>

<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1</version>
</dependency>
<!-- log4j -->
<!-- <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> -->

<!-- 使用 Jackson把Java對象轉換成 JSON -->
<!-- <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency> -->

<!-- mysql數據庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>

<!-- druid數據庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.9</version>
</dependency>
</dependencies>
</project>

就這樣,一個圖書管理系統就實現了 ,雖然只展現了一部分的代碼,但是還是很高興,最起碼我做了,雖然只顯示了部分的功能,但是只要努力了就會有回報。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM