文章目錄
- 一,改造JDBC釋放資源
- --1,完善工具類
- --2,修改測驗類
- 二,HTML
- --1,入門案例
- --2,常用標簽
- --3,表格標簽
- --4,表單標簽
- --5,提交資料
- 作業
- 制作表單
一,改造JDBC釋放資源
–1,完善工具類
package cn.tedu.jdbc;
import java.sql.*;
//提供豐富的方法,方便的jdbc操作
public class JDBCUtils {
//1,獲取資料庫的連接(注冊驅動+獲取連接)
/**
* 獲取資料庫的連接
* @return 資料庫的連接物件Connection
* @throws Exception
*/
static public Connection getConnection() throws Exception{
//1,注冊驅動
Class.forName("com.mysql.jdbc.Driver");//全路徑
//2,獲取資料庫的連接(用戶名/密碼)
//jdbc連接mysql資料庫的協議//本機:埠號/資料庫的名字 解決中文亂碼 指定時區 關閉權限檢驗
String url="jdbc:mysql://localhost:3306/cgb2108?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false" ;
Connection c = DriverManager.getConnection(
url,"root","root");
return c ;//回傳給呼叫者
}
/**
* 釋放資源,提取了長長的代碼
* @param r 結果集資源
* @param s 傳輸器資源
* @param c 連接資源
*/
static public void close(ResultSet r, PreparedStatement s,Connection c){
if(r != null){ //避免了空指標例外
try {
r.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(s != null) {
try {
s.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(c != null) {
try {
c.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
–2,修改測驗類
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//JDBC的練習
public class Test3 {
public static void main(String[] args) throws Exception{
method();//查詢部門表的<100資料
// method2();//向dept表里插入資料
}
//向dept表里插入資料
//為了資源一定會被釋放?
// 把釋放資源的代碼放入finally里+擴大變數的作用范圍
// +在try里修改變數的默認值null+在finally里進行try catch
private static void method2(){
//擴大變數的作用范圍?因為想讓finally也使用
Connection c = null ;
PreparedStatement p = null;
try{
c = JDBCUtils.getConnection();
//插入資料時怎么決定要幾個問號? 要看表里有幾個欄位需要設定值
String sql = "insert into dept values(?,?,?)" ;
p = c.prepareStatement(sql);
//設定SQL的引數
p.setObject(1,666);
p.setObject(2,"軟體測驗部");
p.setObject(3,"大山西");
//執行SQL
int rows = p.executeUpdate();//執行增刪改的SQL
//TODO 會回傳結果集嗎?回傳了的是啥?
System.out.println("影響的行數是: "+rows);
}catch (Exception e){
System.out.println("出錯啦~");
}finally {//資源的釋放是一定要執行的
//呼叫工具類里的close(),增刪改沒有結果集,就不關閉結果集了,傳入null就行了
JDBCUtils.close(null,p,c);
}
}
//查詢部門表的<100資料
private static void method() {
Connection c =null;
PreparedStatement s =null;
ResultSet r =null;
try{
c = JDBCUtils.getConnection();//利用工具類,獲取資料庫的連接
//獲取傳輸器,執行SQL骨架
String sql = "select * from dept where deptno < ?";
s = c.prepareStatement(sql);
//設定SQL的引數
s.setInt(1,100);//給第一個?設定100
r = s.executeQuery();//執行查詢的SQL陳述句
//處理結果集
while(r.next()){//next()判斷有資料嗎
//獲取資料getXxx()--獲取表里的dname欄位的值,并列印
String str = r.getString("dname");
System.out.println(str);
}
}catch (Exception e){
//專案上線階段,給出的解決方案,比如輸出
System.out.println("資料庫連接出錯~~");
//專案開發除錯階段,給出的解決方案,根據報錯資訊
e.printStackTrace();
}finally {//關閉資源
JDBCUtils.close(r,s,c);//呼叫工具類里的close()
}
}
}
二,HTML
–1,入門案例
<!--常用的快捷鍵:保存ctrl s,復制粘貼ctrl cv,剪切ctrl x,移動ctrl 箭頭-->
<!DOCTYPE html> <!-- 檔案宣告行,宣告檔案的型別 -->
<html> <!-- 標志著這是HTML檔案,要有開始標簽和結束標簽-->
<head> <!-- 是網頁的頭部分,設定網頁的標題和編碼-->
<meta charset="utf-8"><!-- 設定了網頁的編碼,u8避免中文亂碼-->
<title>這是測驗檔案</title> <!-- 設定了網頁的標題-->
</head>
<body> <!-- 是網頁的體部分,用來做展示-->
你好 html~ <br/> <!-- br是換行標簽, 是一個空格-->
你好 html~ <br/>
你好 ht ml~
</body>
</html>
–2,常用標簽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測驗 常用標簽</title>
</head>
<body>
<!-- 1.標題標簽:在網頁中顯示標題內容,h1大~h6小 -->
<h1>1號標題</h1>
<h2>2號標題</h2>
<h3>3號標題</h3>
<h4>4號標題</h4>
<h5>5號標題</h5>
<h6>6號標題</h6>
<!-- 2.串列標簽:在網頁中添加串列內容,ul+li/ol+li -->
<ul> <!-- 無序串列 unorderlist-->
<li>直擊山西水災:住房地基塌陷</li>
<li>經濟日報解讀美團壟斷案</li>
</ul>
<ol> <!-- 有序串列 orderlist-->
<li>直擊山西水災:住房地基塌陷</li>
<li>經濟日報解讀美團壟斷案</li>
</ol>
<!-- 3.圖片標簽:在網頁中插入圖片
src是img標簽的屬性,用來指定圖片的路徑
width是img標簽的屬性,用來指定圖片的寬度,單位是px像素
height是img標簽的屬性,用來指定圖片的高度,單位是px像素
-->
<img src="a.png"/>
<img src="2.jpg" width="300px" height="500px"/>
<br />
<!-- 4.超鏈接標簽:給元素添加鏈接效果
href屬性用來指定跳轉目標
target屬性用來指定網頁的打開方式.默認是_self用當前視窗打開,_blank用新視窗
-->
<a href="https://www.baidu.com/" target="_blank">百度一下</a>
<!-- 給圖片添加鏈接效果 -->
<a href="https://www.baidu.com/" target="_blank">
<img src="a.png" />
</a>
<!-- 給串列添加鏈接效果 -->
<ol> <!-- 有序串列 orderlist-->
<li> <a href="https://www.baidu.com/s?cl=3&tn=baidutop10&fr=top1000&wd=%E7%9B%B4%E5%87%BB%E5%B1%B1%E8%A5%BF%E6%B0%B4%E7%81%BE%3A%E4%BD%8F%E6%88%BF%E5%9C%B0%E5%9F%BA%E5%A1%8C%E9%99%B7&rsv_idx=2&rsv_dl=fyb_n_homepage&hisfilter=1">直擊山西水災:住房地基塌陷</a> </li>
<li> <a href="#">經濟日報解讀美團壟斷案</a> </li>
</ol>
<!-- 錨定:回到頂部 -->
<a name="top">我是頂部</a>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<h3>北京富婆通訊錄</h3>
<a href="#top">點擊,回到頂部</a> <!-- 回到name=top的位置處-->
<!-- 5.輸入框標簽: -->
普通的輸入框: <input type="text" />
密碼輸入框: <input type="password" />
數字輸入框: <input type="number" />
年月日輸入框: <input type="date" />
周輸入框: <input type="week" />
按鈕:
<input type="button" value="保存"/>
<button>登錄</button>
提交按鈕:把前端頁面輸入的資料提交給后端java程式處理
<input type="submit" value="注冊"/>
<button type="submit">提交</button>
單選:<input type="radio" />男
多選:<input type="checkbox"/>楊冪
<input type="checkbox"/>迪麗熱巴
<input type="checkbox"/>Anglelababa
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</body>
</html>
–3,表格標簽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測驗 表格表單標簽</title>
</head>
<body>
<!-- 1.練習:向網頁中插入三行三列的表格
是有結構的:table表格,tr行,td列
border屬性用來設定表格的邊框寬度,bordercolor邊框的顏色
width屬性用來設定表格的寬度,bgcolor屬性用來設定表格的背景色
cellspacing屬性用來設定表格里單元格的間距
單元格的合并:行合并rowspan/列合并colspan
-->
<table border="2px" width="500px" bgcolor="pink"
bordercolor="yellow" cellspacing="0">
<tr>
<td colspan="2">11</td>
<td>13</td>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td rowspan="2">23</td>
</tr>
<tr>
<td>31</td>
<td>32</td>
</tr>
</table>
<!-- 2.表格的練習 -->
<h1>流量調查表</h1>
<table border="1px" bordercolor="green">
<tr>
<th>總頁面流量</th>
<th>共計來訪</th>
<th>會員</th>
<th>游客</th>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td>23</td>
<td>24</td>
</tr>
<tr>
<td>31</td>
<td>32</td>
<td>33</td>
<td>34</td>
</tr>
<tr>
<td>41</td>
<td>42</td>
<td>43</td>
<td>44</td>
</tr>
<tr>
<td>平均每人瀏覽</td>
<td colspan="3">1.58</td>
</tr>
</table>
</body>
</html>
–4,表單標簽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測驗 表單標簽</title>
</head>
<body>
<!--1.表單:本質就是表格,只有表單才能提交資料(把瀏覽器輸入的資料交給java程式處理)-->
<form>
<h1>注冊表單</h1>
<table bgcolor="lightgray" border="1px"
bordercolor="green" cellspacing="0"
width="500px">
<tr>
<td>用戶名:</td>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>密碼:</td>
<td>
<input type="password" />
</td>
</tr>
<tr>
<td>確認密碼:</td>
<td>
<input type="password" />
</td>
</tr>
<tr>
<td>昵稱:</td>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>郵箱:</td>
<td>
<input type="email" />
</td>
</tr>
<tr>
<td>性別:</td>
<td>
<input type="radio" />男
<input type="radio" />女
</td>
</tr>
<tr>
<td>愛好:</td>
<td>
<input type="checkbox" />籃球
<input type="checkbox" />足球
<input type="checkbox" />乒乓球
</td>
</tr>
<tr>
<td>城市:</td>
<td>
<select> <!-- 定義下拉框-->
<option>-請選擇-</option> <!-- 定義下拉選項-->
<option>北京</option>
<option>廣東</option>
</select>
</td>
</tr>
<tr>
<td>頭像:</td>
<td>
<input type="file" />
</td>
</tr>
<tr>
<td>驗證碼:</td>
<td>
<input type="text" />
<img src="a.png" />
<input type="button" value="點我換一張"/>
</td>
</tr>
<tr>
<td>自我描述:</td>
<td>
<textarea></textarea> <!-- 文本域-->
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" />
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
–5,提交資料
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測驗 表單標簽</title>
</head>
<body>
<!--1.表單:本質就是表格,只有表單才能提交資料(把瀏覽器輸入的資料交給java程式處理)
form標簽專門用來提交資料:form標簽 + 必須有submit按鈕 + 必須配置name屬性
地址欄: http://127.0.0.1:8848/cgb2108/test3.html?user=jack
?用來拼接用戶輸入的資料,user=jack,其中user是給標簽配置的name屬性的值,jack是用戶從瀏覽器上輸入資料
-->
<form>
<h1>注冊表單</h1>
<table bgcolor="lightgray" border="1px"
bordercolor="green" cellspacing="0"
width="500px">
<tr>
<td>用戶名:</td>
<td>
<input type="text" name="user"/>
</td>
</tr>
<tr>
<td>密碼:</td>
<td>
<input type="password" name="pwd" />
</td>
</tr>
<tr>
<td>確認密碼:</td>
<td>
<input type="password" name="repwd"/>
</td>
</tr>
<tr>
<td>昵稱:</td>
<td>
<input type="text" name="nick"/>
</td>
</tr>
<tr>
<td>郵箱:</td>
<td>
<input type="email" name="mail" />
</td>
</tr>
<tr>
<td>性別:</td>
<td> <!-- 問題1:提交的資料都是on,加value屬性,區分提交的資料-->
<input type="radio" name="sex" value="1"/>男
<input type="radio" name="sex" value="0"/>女
</td>
</tr>
<tr>
<td>愛好:</td>
<td>
<input type="checkbox" />籃球
<input type="checkbox" />足球
<input type="checkbox" />乒乓球
</td>
</tr>
<tr>
<td>城市:</td>
<td>
<select> <!-- 定義下拉框-->
<option>-請選擇-</option> <!-- 定義下拉選項-->
<option>北京</option>
<option>廣東</option>
</select>
</td>
</tr>
<tr>
<td>頭像:</td>
<td>
<input type="file" />
</td>
</tr>
<tr>
<td>驗證碼:</td>
<td>
<input type="text" />
<img src="a.png" />
<input type="button" value="點我換一張"/>
</td>
</tr>
<tr>
<td>自我描述:</td>
<td>
<textarea></textarea> <!-- 文本域-->
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" />
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
作業
制作表單

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307223.html
標籤:java
上一篇:Python技法-序列拆分
