本文實(shí)例為大家分享了java學(xué)生信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
本例的學(xué)生信息添加進(jìn)入數(shù)據(jù)庫的事務(wù)(可以提交事務(wù),事務(wù)回滾,用本地線程完善)
主頁面index.jsp
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> < html > < head > < title >學(xué)生信息管理</ title > </ head > < body > < a href='<c:url value = "/query" />?cmd=query'>查看學(xué)生信息</ a > < br >< br > <!-- <a href="<c:url value='/StudServlet?cmd=save' />">學(xué)生信息添加</a> --> < h2 >學(xué)生信息添加</ h2 > < form action='<c:url value = "/query" />?cmd=add' method="post"> 姓名:< input type = "text" name = "name" />< br >< br > < fieldset style = "border: solid;border-color: red;width: 250px;" > < legend >圖書1</ legend > 書名:< input type = "text" name = "book" />< br >< br > 價(jià)格:< input type = "text" name = "price" /> </ fieldset > < br > < fieldset style = "border: solid;border-color:green;width: 250px;" > < legend >圖書2</ legend > 書名:< input type = "text" name = "book" />< br >< br > 價(jià)格:< input type = "text" name = "price" /> </ fieldset > < br >< br > < input type = "submit" value = "提交" />< br >< br > </ form > </ body > </ html > |
獲取數(shù)據(jù)庫連接的工具ConnUtils5.java
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package cn.hncu.utils; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class ConnUtils5 { //本地線程管理對(duì)象,用于實(shí)現(xiàn): 同一個(gè)線程獲取的連接是同一個(gè) private static ThreadLocal< Connection> t= new ThreadLocal<Connection>(); private final static List<Connection> pool= new ArrayList<Connection>(); private static int SIZE; //由資源文件讀取 private ConnUtils5(){ } static { Properties p= new Properties(); try { //下面這種方式在純Java項(xiàng)目中可以讀取到classpath下的資源文件,但無法讀取JavaEE項(xiàng)目的。因?yàn)門omcat把系統(tǒng)的默認(rèn)類加載器改了 //p.load( ClassLoader.getSystemClassLoader().getSystemResourceAsStream("jdbc.properties")); // p.load(ClassLoader.getSystemResourceAsStream("jdbc.properties")); //讀取Web項(xiàng)目的classpath下的資源文件,用這個(gè)可以 p.load(ConnUtils3. class .getClassLoader().getResourceAsStream( "jdbc.properties" )); String driver=p.getProperty( "driver" ); String url=p.getProperty( "url" ); String name=p.getProperty( "username" ); String pwd=p.getProperty( "password" ); String ssize=p.getProperty( "size" ); SIZE=Integer.parseInt(ssize); Class.forName(driver); for ( int i= 0 ;i<SIZE;i++){ final Connection con=DriverManager.getConnection(url,name,pwd); System.out.println( "con==" +con); //更改conn.close()方法 //用代理模式生成一個(gè)增強(qiáng)版的conn對(duì)象,把它的close()方法攔截更改掉 Object nCon=Proxy.newProxyInstance( ConnUtils3. class .getClassLoader(), // conn.getClass().getInterfaces(), //后面這種方式不行,應(yīng)該是驅(qū)動(dòng)中的實(shí)現(xiàn)類和我們當(dāng)前程序不在同一空間(類加載器不同) new Class[]{Connection. class }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals( "close" )){ System.out.println( "還回一個(gè)鏈接:" +(Connection)proxy); pool.add((Connection)proxy); return null ; } return method.invoke(con, args); } }); pool.add((Connection)nCon); } } catch (Exception e) { e.printStackTrace(); } } public static synchronized Connection getConnection(){ //先從t中拿,如果有就拿出去,如果沒有再到池中拿且把該對(duì)象放到t中 Connection con=t.get(); if (con== null ){ if (pool.size()<= 0 ){ System.out.println( "池中連接沒有了..." ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } return getConnection(); } con=pool.remove( 0 ); t.set(con); //放到t中 } return con; //拿一個(gè)移一個(gè) } } |
資源文件jdbc.properties
1
|
2
3
4
5
6
7
8
9
10
11
|
##MySQL driver=com.mysql.jdbc.Driver url=jdbc: mysql://127.0.0.1:3306/hncu?useUnicode= true &characterEncoding=utf-8 username=root password =1234 size =3 ##Oracle #driver=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@127.0.0.1:1521:orcl #username=scott # password =tiger |
值對(duì)象
Stud.java
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package cn.hncu.domain; import java.util.ArrayList; import java.util.List; /* * 一對(duì)多中的 “一”方 值對(duì)象的建法 */ public class Stud { private String id; private String name; //※專為“多”方添加一個(gè)集合---體現(xiàn)多表中的“一對(duì)多關(guān)系” private List<Book> books= new ArrayList<Book>(); //注意,該集合要在構(gòu)造時(shí)或之前就new出來。 public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this .books = books; } @Override public String toString() { return "id=" + id + "," + name + "," + books; } } |
Book.java
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package cn.hncu.domain; /* * 一對(duì)多中的 “多”方 值對(duì)象的建法 */ public class Book { private Integer id; //基本數(shù)據(jù)類型全部用包裝類的聲明,為以后使用框架做技術(shù)準(zhǔn)備---包裝類能夠兼容框架(因?yàn)橐话憧蚣芏紩?huì)使用類反射) private String name; private Double price; //※專為“一”方添加一個(gè)對(duì)象類型的變量(注意,不用studid)---體現(xiàn)多表中的“一對(duì)多關(guān)系” private Stud s;//設(shè)置主人 //private String studid;//★★不要這樣設(shè) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Stud getS() { return s; } public void setS(Stud s) { this.s = s; } /* * 多表關(guān)聯(lián)時(shí)的toString()方法要注意一個(gè)陷阱,就是一方輸出另一方,同時(shí)另一方又反過來輸出前一方,形成無窮遞歸! */ @Override public String toString() { return "id=" + id + "," + name + "," + price; //這里不能輸出Stud對(duì)象,否則無窮遞歸 } } |
stud層的servlet層–QueryServlet.java
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
package cn.hncu.stud.servlet; import java.io.IOException; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.hncu.domain.Book; import cn.hncu.domain.Stud; import cn.hncu.stud.service.IStudService; import cn.hncu.stud.service.StudServiceImpl; public class QueryServlet extends HttpServlet { //注入 IStudService service= new StudServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cmd=request.getParameter( "cmd" ); System.out.println( "cmd:" +cmd); if ( "query" .equals(cmd)){ query(request, response); } else if ( "add" .equals(cmd)){ add(request, response); } } public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Map<String, String>> studs=service.query(); request.setAttribute( "studs" , studs); request.getRequestDispatcher( "/jsps/show.jsp" ).forward(request, response); } public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1收集參數(shù) 2組織參數(shù)(id字段留到dao中去補(bǔ)) String name[]=request.getParameterValues( "name" ); System.out.println(name[ 0 ]); Stud s= new Stud(); s.setName(name[ 0 ]); //圖書信息 String books[]=request.getParameterValues( "book" ); //防護(hù)一下 ---價(jià)格的防護(hù)應(yīng)該也要寫,這里我們偷懶了 if (books== null ||books.length<= 0 ){ return ; } String prices[]=request.getParameterValues( "price" ); for ( int i= 0 ;i<books.length;i++){ Book b= new Book(); b.setName(books[i]); b.setPrice(Double.parseDouble(prices[i])); //※完成兩個(gè)值對(duì)象的“一對(duì)多”關(guān)系的數(shù)據(jù)封裝 s.getBooks().add(b); //一方 b.setS(s); //多方 } //3調(diào)用service層 try { service.save(s); } catch (Exception e) { //導(dǎo)向失敗頁面 } } } |
stud層的service層–
接口:
1
|
2
3
4
5
6
7
8
9
10
11
12
|
package cn.hncu.stud.service; import java.util.List; import java.util.Map; import cn.hncu.domain.Stud; public interface IStudService { public List<Map<String, String>> query(); public void save(Stud stud); } |
實(shí)現(xiàn)類
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package cn.hncu.stud.service; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Map; import cn.hncu.domain.Stud; import cn.hncu.stud.dao.BookDAO; import cn.hncu.stud.dao.BookJdbcDao; import cn.hncu.stud.dao.StudDAO; import cn.hncu.stud.dao.StudJdbcDAO; import cn.hncu.utils.ConnUtils3; /*我們以后開發(fā)時(shí)通常都要采用一個(gè)dao獨(dú)立操作一個(gè)表,系統(tǒng)中有幾個(gè)實(shí)體表就寫幾個(gè)dao, * 以后框架都是這么干的,我們也要這樣做,因?yàn)榧軜?gòu)好! * * 采用事務(wù)的場合: * 1、如果只有一個(gè)dao,但要執(zhí)行多條sql語句且涉及增刪改,則要開啟事務(wù) * 2、如果一個(gè)service調(diào)用多個(gè)dao,通常也要開啟事務(wù)。 */ public class StudServiceImpl implements IStudService { //注入 StudDAO dao_stud= new StudJdbcDAO(); BookDAO dao_book= new BookJdbcDao(); @Override public List<Map<String, String>> query() { return dao_stud.query(); } @Override public void save(Stud stud) { Connection con= null ; try { con=ConnUtils3.getConnection(); System.out.println( "拿到一個(gè)鏈接:" +con); con.setAutoCommit( false ); dao_stud.save(stud); dao_book.save(stud.getBooks()); System.out.println( "提交一個(gè)事務(wù)..." ); con.commit(); } catch (Exception e) { try { System.out.println( "回滾一個(gè)事務(wù)..." ); con.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { con.setAutoCommit( true ); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } |
stud層的dao層–
Stud接口
1
|
2
3
4
5
6
7
8
9
10
11
12
|
package cn.hncu.stud.dao; import java.util.List; import java.util.Map; import cn.hncu.domain.Stud; public interface StudDAO { public List<Map<String, String>> query(); public void save(Stud stud) throws Exception; } |
Stud實(shí)現(xiàn)類
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package cn.hncu.stud.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import cn.hncu.domain.Book; import cn.hncu.domain.Stud; import cn.hncu.utils.ConnUtils3; public class StudJdbcDAO implements StudDAO { @Override public List<Map<String, String>> query() { List<Map<String, String>> list= new ArrayList<Map<String,String>>(); //一個(gè)map就是一行數(shù)據(jù), List<Map>就是整個(gè)數(shù)據(jù)表 Connection con= null ; try { con=ConnUtils3.getConnection(); Statement st=con.createStatement(); String sql= "select * from stud" ; ResultSet rs=st.executeQuery(sql); while (rs.next()){ Map<String,String> m= new HashMap<String, String>(); m.put( "id" , (String) rs.getObject( 1 )); m.put( "name" , (String) rs.getObject( 2 )); list.add(m); } rs.close(); st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } @Override public void save(Stud stud) throws Exception { Connection con=ConnUtils3.getConnection(); System.out.println( "拿到一個(gè)鏈接:" +con); String sql= "insert into stud values(?,?)" ; String uuid=UUID.randomUUID().toString().replace( "-" , "" ); PreparedStatement pst=con.prepareStatement(sql); stud.setId(uuid); //為了"多方"即book能夠拿到"一方"的id,專門補(bǔ)的 pst.setString( 1 , uuid); pst.setString( 2 , stud.getName()); System.out.println( "1:" +uuid+ ",2:" +stud.getName()); pst.executeUpdate(); // con.close();//拿到同一個(gè)con,這里就不需要關(guān)了 } } |
Book接口
1
|
2
3
4
5
6
7
8
9
10
|
package cn.hncu.stud.dao; import java.util.List; import cn.hncu.domain.Book; public interface BookDAO { public void save(List<Book> books) throws Exception; } |
Book實(shí)現(xiàn)類
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package cn.hncu.stud.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.List; import cn.hncu.domain.Book; import cn.hncu.utils.ConnUtils3; public class BookJdbcDao implements BookDAO { @Override public void save(List<Book> books) throws Exception { Connection con=ConnUtils3.getConnection(); System.out.println( "拿到一個(gè)鏈接:" +con); String sql= "insert into book(name,price,studid) values(?,?,?)" ; PreparedStatement pst=con.prepareStatement(sql); for (Book b:books){ pst.setString( 1 , b.getName()); pst.setDouble( 2 , b.getPrice()); pst.setObject( 3 , "12132312" ); //異常(故意給一個(gè)不存在的外鍵字段,以測試事務(wù)回滾)--測事務(wù)回滾 // pst.setObject(3, b.getS().getId()); System.out.println( "1:" +b.getName()+ ",2:" +b.getPrice()+ ",3:" +b.getS().getId()); pst.addBatch(); //添加到批處理 } pst.executeBatch(); //執(zhí)行批處理 // con.close();//這里拿到同一個(gè)con,這里不需要關(guān) } } |
顯示學(xué)生信息頁面jsps/show.jsp
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> < html > < head > < title >學(xué)生信息管理</ title > </ head > < body > < h2 >學(xué)生信息</ h2 > < c:forEach items = "${studs}" var = "x" > ${x.id},${x.name}< br /> </ c:forEach > </ body > </ html > |
效果圖:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。