Вы находитесь на странице: 1из 27

-

MySQL
: (Troy Giunipero)
-,
MySQL.
-, JavaServer Pages (JSP),
JavaServer Pages (JSTL), API Java (JDBC)
.
, MySQL.
MySQL ,
- ,
. MySQL SQL ( )
.

MySQL , MySQL
MyNewDatabase, IDE NetBeans.
, , ifpwafcad.sql
. SQL
Subject Counselor .
, IDE NetBeans
MySQL MyNewDatabase.


,
, -. Java MySQL
API Java. , MySQL Connector/J JDBC
Java,
(GlassFish), SQL, ,
(MySQL).
, , JSP.
HTML CSS ,
JSTL ,
. MySQL
MyNewDatabase Subject Counselor.
.

index.jsp
HTML. index.jsp JSTL
MyNewDatabase. Subject
.
HTML
(response.jsp). JSTL MyNewDatabase.
- Subject Counselor.
,
.

IFPWAFCAD
.
index.jsp

response.jsp


- IDE:
1. " > " (CTRL+SHIFT+N) .
"Java Web", "-".
"".

-
IDE. ,
IDE Ant.
2. " " "IFPWAFCAD".
. IDE
NetBeansProjects . "".
3. " " GlassFish ,
.
. GlassFish '',
Java IDE NetBeans. GlassFish

, IDE .
""
"" IDE.
, GlassFish
.
4. " Java EE" Java EE 5.

- Java EE 6 web.xml
. NetBeans web.xml Java EE
6.
. Java EE 6
, Java EE 5.
. Java EE 6,
web.xml .
"Web", "
".
5. "".
JSP (index.jsp). index.jsp
.
Sun Java BluePrints.

-
(index.jsp)
(response.jsp). HTML
.
HTML.
, .

, index.jsp. ,
index.jsp "IFPWAFCAD" > "-" "".
1. <title>
IFPWAFCAD.
2. <h1> IFPWAFCAD,

!

3. IDE, "" > ""


(CTRL+SHIFT+8; &#8984+SHIFT+8 Mac).
"" "HTML" ,
.

' '
' ', .
4. <h1>. (
HTML). "" "".
5. " "
"":
o : 2
o : 1
o : 0
HTML .
6.
( ):
<table border="0">
<thead>
<tr>
<th>IFPWAFCAD offers expert counseling in a wide range of
fields.</th>
</tr>
</thead>
<tbody>
<tr>
<td>To view the contact details of an IFPWAFCAD certified
former professional wrestler in your area, select a subject below:</td>
</tr>

7. HTML.
<td> HTML (
)
. " " response.jsp

"" "".

8. <form> (
):
<tr>

<td>

<form action="response.jsp">
<strong>Select a subject:</strong>
</form>
</td>
</tr>

""
IDE.
CTRL+.
.
9.
ENTER, CTRL+
IDE.
.
10. " "
subject_id "" "".
, .
.
JSTL
, "Subject".
11. '' ( ) ,
.
""
, . " "
"" "" .
12.
'' (Alt-Shift-F; Ctrl-Shift-F Mac).
:
<body>
<h2>Welcome to <strong>IFPWAFCAD</strong>,
the International Former Professional Wrestlers' Association for
Counseling and Development!

</h2>
<table border="0">
<thead>
<tr>
<th>IFPWAFCAD offers expert counseling in a wide range of
fields.</th>
</tr>
</thead>
<tbody>
<tr>
<td>To view the contact details of an IFPWAFCAD certified
former
professional wrestler in your area, select a subject
below:</td>
</tr>
<tr>
<td>
<form action="response.jsp">
<strong>Select a subject:</strong>
<select name="subject_id">
<option></option>
</select>
<input type="submit" value="submit"
name="submit" />
</form>
</td>
</tr>
</tbody>
</table>
</body>


' ' (Shift-F6; Fn-Shift-F6 Mac).
JSP .
,
, .


response.jsp
. , ,
, JSP.
-, JSP.
1. "IFPWAFCAD" ""
" > JSP". " JSP".

2. " JSP" response. ,


"" "-",
, web. ,
index.jsp.
3. "".

response.jsp. JSP "-"
"".

4. IFPWAFCAD - {}
5. <h1>Hello World!</h1> <body>.
HTML:
<table border="0">
<thead>
<tr>
<th colspan="2">{placeholder}</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Description: </strong></td>
<td><span style="font-size:smaller; fontstyle:italic;">{placeholder}</span></td>
</tr>
<tr>
<td><strong>Counselor: </strong></td>
<td>{placeholder}
<br>
<span style="font-size:smaller; font-style:italic;">
member since: {placeholder}</span>
</td>
</tr>
<tr>
<td><strong>Contact Details: </strong></td>
<td><strong>email: </strong>
<a href="mailto:{placeholder}">{placeholder}</a>
<br><strong>phone: </strong>{placeholder}
</td>
</tr>
</tbody>
</table>


' ' (Shift-F6; Fn-Shift-F6 Mac).
GlassFish


-.
,
HTML
index.jsp response.jsp.
1. , ' ' (
)
IDE. "Web",
" " "".
2. style " CSS" "". IDE
CSS ,
index.jsp response.jsp. , style.css
"",
.
3. style.css:
body {
font-family: Verdana, Arial, sans-serif;
font-size: smaller;
padding: 50px;
color: #555;
}
h1 {

text-align: left;
letter-spacing: 6px;
font-size: 1.4em;
color: #be7429;
font-weight: normal;
width: 450px;

table {
width: 580px;
padding: 10px;
background-color: #c5e7e0;
}
th {
text-align: left;
border-bottom: 1px solid;

}
td {

padding: 10px;

}
a:link {
color: #be7429;
font-weight: normal;
text-decoration: none;
}
a:link:hover {
color: #be7429;
font-weight: normal;
text-decoration: underline;
}
4. index.jsp

response.jsp.
<head>:
<link rel="stylesheet" type="text/css" href="style.css">


CTRL+TAB, .

CSS NetBeans
CSS IDE "
CSS" " ".
.
, h1 style.css,
" CSS" (" > > CSS"):

" CSS"
. ,
,
IDE .


.
, ,
.
, .
, ,
. , ,
, .

. ,
web.xml. ,
(JDBC MySQL
Connector/J).
. ,
MySQL MyNewDatabase
ifpwafcad.sql. SQL Subject Counselor
. ,
, ,
MySQL.
GlassFish
.
MySQL root
.
nbuser.
nbuser MySQL bin
:
shell> mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('nbuser')
->
WHERE User = 'root';
mysql> FLUSH PRIVILEGES;

JDBC
GlassFish Server Open Source Edition
(DBCP)
.
JDBC ( Java)
.

GlassFish , ,
sun-resources.xml.
.
,
.
JDBC NetBeans.

1. , ' ' (
)
IDE. GlassFish,
" JDBC" "".
2. 2, " " "
JDBC", " JNDI" jdbc/IFPWAFCAD.

JDBC JNDI. API JNDI



. JND.
3. . ,

,
IFPWAFCAD.

4. "". ""
3, " ".
5. 4 JDBC IfpwafcadPool. ,
" "
jdbc:mysql://localhost:3306/MyNewDatabase .

. ,
IDE.
MyNewDatabase. , ,
'' (Ctrl-5) ( )
' '.
6. "". 5 , IDE
,
"name-value" .

7. "". glassfishresources.xml,
.
'' ' ' >
glassfish-resources.xml. , <resources>

.
. IDE NetBeans 6.9 IDE, ,
IDE sun-resources.xml.


GlassFish, ,
"" IDE.
1. '', IFPWAFCAD
''. , ,
.
2. "" (CTRL+5; &#8984+5 Mac OS)
"" > "GlassFish" > "" > "JDBC" > "
JDBC". ,
:


- JDBC.
web.xml.
XML,
.
,
, , ,
.
. Java EE 6 Java ,
, 'Web' > '
' .
,
.
1. '' ' '
web.xml. IDE .
2. "" .
3. " " "",
" ".
4. " " ,
(jdbc/IFPWAFCAD). ""
, , ,

"IFPWAFCAD".

"".

"".
" ".

5. , web.xml
"XML" . ,
<resource-ref>.
<resource-ref>
<description>Database for IFPWAFCAD application</description>
<res-ref-name>jdbc/IFPWAFCAD</res-ref-name>
<res-type>javax.sql.dataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

JAR
JAR ,
.
mysql-connector-java-5.1.6.xbin.jar .

IDE ,
JAR, , .
, "" ( "" >
""). IDE " JDBC".
, ,
. MySQL, ,
, IDE
.
1. "" " > "".
"GlassFish".
2. " JDBC".

3. "".
GlassFish IDE
.
, " "
. , JAR
domain1. ,
GlassFish.
4. "" "".
5. GlassFish
domains > domain1 > lib. IFPWAFCAD
, mysql-connector-java-5.1.6bin.jar. JAR , .
6. . '' IDE ''
. ""
IDE (CTRL+4). "" , MySQL
GlassFish.

domain1/lib , mysqlconnector-java-5.1.6.x-bin.jar .


index.jsp response.jsp,
. JSTL (. .
) .
.

JSTL

JavaServer Pages (JSTL). GlassFish
JSTL. IFPWAFCAD "" > "
GlassFish" "" jstl-impl.jar.
GlassFish ,
.
JSTL . :

core: ,

,

fmt:
sql:
xml: XML

core sql.

JSTL

. SQL
, .
IDE JSTL,
"" (CTRL+SHIFT+8).

""
CTRL+ .
CTRL+ "db".
index.jsp
index.jsp
"Subject":

1. " " "".

" " SQL <sql:query>,


<c:forEach> resultset ,
.
2. <%@page ... %> ( 7)
" " "".
:
o : subjects
o :
o : jdbc/IFPWAFCAD
o : SELECT subject_id, name FROM Subject

3. "". index.jsp .
.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>


<%-Document
: index
Created on : Dec 22, 2009, 7:39:49 PM
Author
: nbuser
--%>
<sql:query var="subjects" dataSource="jdbc/IFPWAFCAD">
SELECT subject_id, name FROM Subject
</sql:query>
<table border="1">
<!-- column headers -->
<tr>
<c:forEach var="columnName" items="${subjects.columnNames}">
<th><c:out value="${columnName}"/></th>
</c:forEach>
</tr>
<!-- column data -->
<c:forEach var="row" items="${subjects.rowsByIndex}">
<tr>
<c:forEach var="column" items="${row}">
<td><c:out value="${column}"/></td>
</c:forEach>
</tr>
</c:forEach>
</table>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

, IDE
taglib, JSTL, ,
(<sql:query> <c:forEach>). taglib , JSP
(JSTL), ,
, .
4. , .
"" "".
"", IDE GlassFish,
,
. , " ",
.

, " "

. .

HTML,
.
5. . <c:forEach>,
. JSP (
GlassFish)
. .
6. <c:forEach> HTML .
subject_id, - name,
. ( ).
<form action="response.jsp">
<strong>Select a subject:</strong>
<select name="subject_id">
<c:forEach var="row" items="${subjects.rowsByIndex}">
<c:forEach var="column" items="${row}">
<option value="<c:out value="${column}"/>"><c:out value="$
{column}"/></option>
</c:forEach>
</c:forEach>
</select>
<input type="submit" value="submit" name="submit" />
</form>

<c:forEach> HTML.
<form action="response.jsp">
<strong>Select a subject:</strong>
<select name="subject_id">
<c:forEach var="row" items="${subjects.rows}">
<option value="${row.subject_id}">${row.name}</option>
</c:forEach>
</select>

<input type="submit" value="submit" name="submit" />


</form>

<c:forEach> subject_id
name SQL HTML <option>.
.
7. , " ". (
.)
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-Document
: index
Created on : Dec 22, 2009, 7:39:49 PM
Author
: nbuser
--%>
<sql:query var="subjects" dataSource="jdbc/IFPWAFCAD">
SELECT subject_id, name FROM Subject
</sql:query>
<table border="1">
<!-- column headers -->
<tr>
<c:forEach var="columnName" items="${subjects.columnNames}">
<th><c:out value="${columnName}"/></th>
</c:forEach>
</tr>
<!-- column data -->
<c:forEach var="row" items="${subjects.rowsByIndex}">
<tr>
<c:forEach var="column" items="${row}">
<td><c:out value="${column}"/></td>
</c:forEach>
</tr>
</c:forEach>
</table>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

8. (CTRK+S; &#8984+S Mac OS).


9. .
, ,
.
, .
" ". ,
,
.
""
"".
response.jsp

,
.
, counselor_id
counselor_idfk .
1. <%@page ... %> ( 7), db
CTRL+. . (
).
2. :
o : counselorQuery
o :
o : jdbc/IFPWAFCAD
o : SELECT * FROM Subject, Counselor WHERE
Counselor.counselor_id = Subject.counselor_idfk AND
Subject.subject_id = ? <sql:param value="${param.subject_id}"/>

3. "". response.jsp .
.
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-Document
: response
Created on : Dec 22, 2009, 8:52:57 PM
Author
: nbuser
--%>
<sql:query var="counselorQuery" dataSource="jdbc/IFPWAFCAD">
SELECT * FROM Subject, Counselor
WHERE Counselor.counselor_id = Subject.counselor_idfk
AND Subject.subject_id = ? <sql:param value="${param.subject_id}"/>

</sql:query>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

, IDE
taglib <sql:query>. ,
<sql:param>.
subject_id, index.jsp,
EL ( ) ${param.subject_id},
<sql:param>.
SQL (?) .
4. <c:set> ,
() resultset, .
.
<sql:query var="counselorQuery" dataSource="jdbc/IFPWAFCAD">
SELECT * FROM Subject, Counselor
WHERE Counselor.counselor_id = Subject.counselor_idfk
AND Subject.subject_id = ? <sql:param value="${param.subject_id}"/>
</sql:query>
<c:set var="counselorDetails" value="${counselorQuery.rows[0]}"/>

, resultset
, ,
EL (
). , index.jsp
resultset <c:forEach>.
<c:forEach> ,

EL.
5. taglib JSTL .
<c:set>. .
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

6. HTML EL
counselorDetails. (
):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8"/>
<link rel="stylesheet" type="text/css" href="style.css">
<title>${counselorDetails.name}</title>
</head>
<body>
<table>
<tr>

<th colspan="2">${counselorDetails.name}</th>
</tr>

<tr>

<td><strong>Description: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">$
{counselorDetails.description}</span></td>
</tr>
<tr>
<td><strong>Counselor: </strong></td>
<td><strong>${counselorDetails.first_name} $
{counselorDetails.nick_name} ${counselorDetails.last_name}</strong>
<br><span style="font-size:smaller; fontstyle:italic;">
<em>member since: $
{counselorDetails.member_since}</em></span></td>
</tr>
<tr>
<td><strong>Contact Details: </strong></td>
<td><strong>email: </strong>
<a href="mailto:${counselorDetails.email}">$
{counselorDetails.email}</a>
<br><strong>phone: </strong>$
{counselorDetails.telephone}</td>
</tr>
</table>
</body>
</html>


.
. , NetBeans
" "
.
.
' ' ( ) .
IDE index.jsp..
index.jsp,
submit. response.jsp,
.

" -,
MySQL"
- MySQL.

JSTL JSP
.



GlassFish Server Open Source Edition
MySQL.
.

?
IDE "" (CTRL+5; &#8984+5 Mac)
MySQL, MyNewDatabase
.

MySQL
MySQL ''.
(
) MyNewDatabase ''
, MySQL
(
) ' '.
.

" "
URL " URL JDBC". , URL
(, jdbc:mysql://localhost:3306/MyNewDatabase),
" URL JDBC". .

, Subject Counselor
, MyNewDatabase(
)
MyNewDatabase ( ).
. ,
' '.

?
GlassFish glassfishresources.xml
JDBC. "" "".

"" > " GlassFish" > "".


"JDBC Resources" ( JDBC),
jdbc/IFPWAFCAD, glassfish-resources.xml.
" ", IfpwafcadPool,
glassfish-resources.xml. .

MySQL Connector/ J GlassFish?


, GlassFish MySQL
Connector/J. JAR
.

GlassFish
GlassFish domains/domain1/lib. mysqlconnector-java-5.1.6-bin.jar.

?
,
GlassFish .

MySQL root
.

nbuser MySQL
bin :

shell> mysql -u root


mysql> UPDATE mysql.user SET Password = PASSWORD('nbuser')
->
WHERE User = 'root';
mysql> FLUSH PRIVILEGES;


MySQL: MySQL.

?
.
1. "" (CTRL+5; &#8984+5 Mac)
".
2. " GlassFish" "
".
3. / admin/adminadmin.
4. "" > "JDBC" > "
" > IfpwafcadPool.
IfpwafcadPool.
5. "Ping". ,
Ping Succeeded.

6. ping- ,
" "
.

Вам также может понравиться