文章目錄
- 一,JDBC
- --1,創建工具類
- --2,模擬用戶登錄
- --3,JDBC的練習
- --4,JDBC的總結
- --5,修改釋放資源的代碼
- 二,HTML
- --1,概述
- --2,入門案例
一,JDBC
–1,創建工具類
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
//提供豐富的方法,方便的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 ;//回傳給呼叫者
}
}
–2,模擬用戶登錄
package cn.tedu.jdbc;
import java.sql.*;
import java.util.Scanner;
//需求:利用JDBC,查詢tb_user表里的資料
/* 1,創建表,并插入資料 2,利用JDBC,查詢資料
CREATE TABLE tb_user(
id int PRIMARY KEY auto_increment,
name varchar(20) default NULL,
password varchar(20) default NULL
)
insert into tb_user values(null,'jack','321')
*/
public class Test2 {
public static void main(String[] args) throws Exception {
// method();//查詢tb_user表里的資料
// method2();//模擬用戶登錄
method3();//解決SQL攻擊問題
}
//解決SQL攻擊問題
private static void method3() throws Exception {
//1,注冊驅動 2,獲取連接
Connection c = JDBCUtils.getConnection();
//3,執行SQL
String a = new Scanner(System.in).nextLine();//用戶名
String b = new Scanner(System.in).nextLine();//密碼
//如果動態的拼接字串時,資料在中間的位置 "+a+"
// String sql="select * from tb_user where name='jack' and password='321'" ;
// String sql="select * from tb_user where name='"+a+"' and password='"+b+"'" ;
//SQL骨架:用?代替了引數的位置,?叫占位符,好處:簡潔(避免了SQL拼接引數)
String sql="select * from tb_user where name=? and password=?" ;
//4,獲取傳輸器
// Statement s = c.createStatement();
PreparedStatement s = c.prepareStatement(sql);
//設定SQL引數--setXxx()設定不同型別的引數
s.setString(1,a);//?的索引,要給?設定的值
s.setString(2,b);//?的索引,要給?設定的值
//TODO 當用戶名輸入jack'#時還會發生SQL攻擊嗎???
ResultSet r = s.executeQuery();
//5,決議結果集
if(r.next()){//查到資料了嗎?查到了就登錄成功
System.out.println("登錄成功~");
}else{
System.out.println("用戶名或者密碼輸入錯誤,登錄失敗~");
}
//6,關閉資源
r.close();
s.close();
c.close();
}
//查詢tb_user表里的資料
private static void method() throws Exception{
//呼叫工具類的方法
Connection c = JDBCUtils.getConnection();
//3,獲取傳輸器
Statement s = c.createStatement();
//4,執行SQL
ResultSet r = s.executeQuery("select * from tb_user");
//5,決議結果集
while(r.next()){//判斷r有資料
//獲取r的資料
int a = r.getInt("id");//獲取表里的id欄位的值
String b = r.getString("name");//獲取表里的name欄位的值
String c1 = r.getString("password");//獲取表里的password欄位的值
System.out.println(a+b+c1);
}
//6,釋放資源
r.close();//釋放結果集
s.close();//釋放傳輸器
c.close();//釋放連接器
}
/* 模擬用戶登錄
1,發起SQL:select * from tb_user where name='jack' and password='321'
2,判斷result,如果有結果就登錄成功,沒結果就登錄失敗
問題: SQL攻擊/SQL注入,
本質上就是因為SQL陳述句中出現了特殊符號(#,注釋掉了一些條件),導致了SQL語意改變了
解決方案:Statement低級的傳輸器,不安全,低效
換成PreparedStatement高級,安全
*/
private static void method2() throws Exception {
//1,注冊驅動 2,獲取連接
Connection c = JDBCUtils.getConnection();
//3,獲取傳輸器
Statement s = c.createStatement();
//4,執行SQL
String a = new Scanner(System.in).nextLine();//用戶名
String b = new Scanner(System.in).nextLine();//密碼
//如果動態的拼接字串時,資料在中間的位置 "+a+"
// String sql="select * from tb_user where name='jack' and password='321'" ;
String sql="select * from tb_user where name='"+a+"' and password='"+b+"'" ;
ResultSet r = s.executeQuery(sql);
//5,決議結果集
if(r.next()){//查到資料了嗎?查到了就登錄成功
System.out.println("登錄成功~");
}else{
System.out.println("用戶名或者密碼輸入錯誤,登錄失敗~");
}
//6,關閉資源
r.close();
s.close();
c.close();
}
}
–3,JDBC的練習
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//JDBC的練習
public class Test3 {
public static void main(String[] args) throws Exception{
// method();//查詢部門表的<100資料
method2();//向dept表里插入資料
}
//向dept表里插入資料
private static void method2() throws Exception {
Connection c = JDBCUtils.getConnection();
//插入資料時怎么決定要幾個問號? 要看表里有幾個欄位需要設定值
String sql = "insert into dept values(?,?,?)" ;
PreparedStatement p = c.prepareStatement(sql);
//設定SQL的引數
p.setObject(1,666);
p.setObject(2,"軟體測驗部");
p.setObject(3,"大山西");
//執行SQL
p.executeUpdate();//執行增刪改的SQL
//TODO 會回傳結果集嗎?回傳了的是啥?
}
//查詢部門表的<100資料
private static void method() throws Exception{
Connection c = JDBCUtils.getConnection();//利用工具類,獲取資料庫的連接
//獲取傳輸器,執行SQL骨架
String sql = "select * from dept where deptno < ?";
PreparedStatement s = c.prepareStatement(sql);
//設定SQL的引數
s.setInt(1,100);//給第一個?設定100
ResultSet r = s.executeQuery();//執行查詢的SQL陳述句
//處理結果集
while(r.next()){//next()判斷有資料嗎
//獲取資料getXxx()--獲取表里的dname欄位的值,并列印
String str = r.getString("dname");
System.out.println(str);
}
//關閉資源
r.close();
s.close();
c.close();
}
}
–4,JDBC的總結
1, 什么是JDBC? java程式連接資料庫的標準方案,全稱是java database connectivity
2, 使用JDBC步驟? 匯入jar包,注冊驅動,獲取資料庫的連接,獲取傳輸器,執行SQL,決議結果集(查詢),關閉資源
3, 傳輸器Statement和PreparedStatement有什么區別? Statement不安全(可能發生SQL攻擊),而且低效
4, Statement和PreparedStatement有什么關系? public interface PreparedStatement extends Statement ,是父子介面
5, SQL攻擊? 原因是:SQL中出現了特殊符號#(注釋符號)改變了SQL的語意 解決方案:使用新的傳輸器PreparedStatement
6, 執行SQL: 先執行SQL骨架,然后再給SQL設定引數
executeUpdate(): 用來執行增刪改的SQL陳述句,并且回傳了影響行數
executeQuery(): 用來執行查的SQL陳述句,并且回傳了結果集ResultSet
–5,修改釋放資源的代碼
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 {//資源的釋放是一定要執行的
//關閉資源
try{
p.close();
}catch (Exception e){
e.printStackTrace();
}
try {
c.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//查詢部門表的<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 {
//關閉資源
try {
r.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
s.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
c.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
二,HTML
–1,概述
全稱是超文本標記語言
超文本:可以向網頁中插入的元素型別豐富(圖片,視頻,音頻,文字…)
標記: HTML提供了各種標記表示是不同型別的元素img video
語法: 由很多的標簽組成的, 標簽要一對的寫要寫開始標簽和結束標簽.
–2,入門案例
1,創建專案:新建專案–設定專案名稱–選擇存放路徑–創建
2,創建HTML檔案: 選中專案–右鍵–新建–HTML檔案–輸入檔案名–創建
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>這是測驗檔案</title>
</head>
<body>
你好 html~ <br/>
你好 html~ <br/>
你好 ht ml~
你好 html~
你好 html~
你好 html~
你好 html~
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/306493.html
標籤:其他
上一篇:Qt開發經驗小技巧176-180
