文章目錄
1、七層協議
應用層:DNS、FTP、HTTP、SSH…
表示層:(數據格式)jpeg、gif、des…
會話層:(定義開始、結束會話)rpc、sql…
傳輸層:TCP、UDP…
網絡層:IP…
數據鏈路層:WIFI、以太網、GPRS、PPP…
物理層:調制解調器、光導纖維…
2、tcp三次握手
3、udp協議特點
非面向連接的協議,速度快不可靠,只管發出去不管是否接收到
一般視頻是用udp協議
4、JDBC介紹
JDBC API 允許用戶訪問任何形式的表格數據,尤其是存儲在關係數據庫中的數據。
執行流程:
- 連接數據源,如:數據庫。
- 爲數據庫傳遞查詢和更新指令。
- 處理數據庫響應並返回的結果。
5、jdbc連接數據庫實例
eclipce自動補全代碼:alt + /
下載mysql的jdbc 的 jar包:https://www.cnblogs.com/twodoge/p/9982696.html
下載完成將jar包導入工程,然後build path
下載安裝mysql :https://www.runoob.com/mysql/mysql-install.html
修改mysql默認密碼:https://www.cnblogs.com/color-zou/p/11316221.html
修改mysql密碼的幾種方式:https://www.cnblogs.com/mrhonest/p/10881646.html
在mysql裏面新建一個表student
jdbc連接數據庫實例
package aa;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class jdbc {
public static void main(String[] args) {
Connection con=null;
Statement state=null;
ResultSet result=null;
try {
//1\加載驅動類
// Class.forName("com.mysql.jdbc.Driver"); //5.x版本的驅動文件jar包對應的是jdbc.Driver,8.0x版本的數據庫驅動文件是.cj.jdbc.Driver
Class.forName("com.mysql.cj.jdbc.Driver");
//2\獲取數據庫連接
// String url="jdbc:mysql://主機:端口號/數據庫名稱"
//第一種
// String url="jdbc:mysql://localhost:3306/student?user=root&password=root&serverTimezone=GMT";
// con=DriverManager.getConnection(url);
//第二種
//mysql賬號密碼也可以做getConnection()參數
// String url="jdbc:mysql://localhost:3306/student?serverTimezone=GMT";
//con=DriverManager.getConnection(url,"root","root");
//第三種
//也可以把賬號密碼單獨Properties文件傳遞
String url="jdbc:mysql://localhost:3306/student?serverTimezone=GMT";
Properties jdbc=new Properties();
jdbc.load(new FileReader(new File("jdbc.properties")));
con=DriverManager.getConnection(url,jdbc);
// System.out.println(con);
//3\準備sql
String sql="select id from s";
//4\提交、執行sql
state=con.createStatement();
result = state.executeQuery(sql);//查詢
//更新/修改/刪除 數據 state.executeUpdate(sql),返回的是int類型,表示影響行數
//能執行所有sql語句state.execute(),返回值布爾,執行的查詢就是True,其他語句false,用此方法需要用r = state.getResultSet()獲取結果集、n = state.getUpdateCount()獲取影響行數。
//5處理結果
//next查詢是否有下一個,沒有返回false
while (result.next()) {
// int id=result.getInt("id");可以按照列名取,也可以按下標
int id=result.getInt(1);
System.out.println(id);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//6釋放資源、異常處理
try {
if(result!=null) {
result.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (state!=null) {
state.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(con!=null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
jdbc連接數據庫實例(代碼縮減版)
package aa;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbc2 {
public static void main(String[] args) {
Connection con = null;
Statement state = null;
ResultSet result = null;
try {
// 1\加載驅動類
Class.forName("com.mysql.cj.jdbc.Driver");
// 2\獲取數據庫連接
String url = "jdbc:mysql://localhost:3306/student?user=root&password=root&serverTimezone=GMT";
con = DriverManager.getConnection(url);
// 3\準備sql
String sql = "select id from s";
// 4\提交、執行sql
state = con.createStatement();
result = state.executeQuery(sql);
// 5處理結果
while (result.next()) {
int id = result.getInt(1);
System.out.println(id);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6釋放資源、異常處理
try {
if (result != null) {
result.close();
}
if (state != null) {
state.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
拼接sql語句
String name="小明";
String sql="delete from stuent where name='"+name+"'";
鍵盤輸入(這裏容易被sql注入)
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
String sql="delete from stuent where name='"+name+"'";
爲防止sql注入,一般採用預編譯PrepareStatement
PrepareStatement state=null;
// 3\準備sql
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
String sql="delete from stuent where name=?";
// 4\提交、執行sql
state=con.prepareStatement(sql);
steta.setString(1,name);
state.excute();
//prepareStatement可以防止sql注入,除第一次執行外,以後的執行效率比Statement高
5.1、存儲過程的調用CallableStatement
先在mysql裏面新建存儲過程
CREATE PROCEDURE pro_demo(IN paccount CHAR(11),OUT ppassword VARCHAR(32))
BEGIN
SELECT `password` INTO ppassword FROM s WHERE id=paccount;
END
CALL pro_demo(1,@p);
SELECT @P;
代碼
package aa;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class jdbc2 {
public static void main(String[] args) {
Connection conn = null;
ResultSet result = null;
CallableStatement state=null;
try {
// 1\加載驅動類
Class.forName("com.mysql.cj.jdbc.Driver");
// 2\獲取數據庫連接
String url = "jdbc:mysql://localhost:3306/student?user=root&password=root&serverTimezone=GMT";
conn= DriverManager.getConnection(url);
//3sql
String sql="{CALL pro_demo(?,?)}";
//4創建狀態,提交併執行
state=conn.prepareCall(sql);
// 設置輸入參數
state.setInt(1,1);
//註冊輸出參數類型
state.registerOutParameter(2, java.sql.Types.VARCHAR);
//執行sql
state.execute();
//處理結果,獲取輸出參數(取第二個問號)
String p = state.getString(2);
System.out.println(p);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6釋放資源、異常處理
try {
if (result != null) {
result.close();
}
if (state != null) {
state.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
5.2、事務控制
6、tomcat安裝
tomcat下載地址:https://tomcat.apache.org/
安裝教程:https://www.cnblogs.com/limn/p/9358657.html
啓動tomcat:找到根目錄下bin/startup.bat雙擊啓動
大致步驟:下載-解壓-配置環境變量-修改配置文件-測試http://localhost:8080
在eclipse裏面配置tomcat:https://blog.csdn.net/zs20082012/article/details/79138204
7、新建web項目
創建完成,生成web.xml
8、創建LoginAction類
在src目錄下新建LoginServlet
在LoginServlet中繼承HttpServlet,重寫destroy()、init()、service(HttpServletRequest request, HttpServletResponse response) ,再添加一個構造方法
package com.web01;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
System.out.println("實例化");
}
public void destroy() {
super.destroy();
System.out.println("銷燬");
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
System.out.println("服務");
}
public void init() throws ServletException{
System.out.println("初始化");
}
}