最新在线看的黄网|伊人久久久久久久网站|日本a视频专区二|欧美A级无码毛片|有扫五av在线播放|好屌日aV在线播放|内射福利视频导航|极品少妇一区二区视频|无吗在线一区播放|性爱黄色视频不卡

JAVA-MySQL再戰(zhàn)關(guān)系型數(shù)據(jù)庫

Java與Mysql基礎(chǔ)應(yīng)用

確保已有java jdk

確保已安裝mysql

1.百度搜索 mysql-connector-java-8.0.21.jar 并下載

2.在項(xiàng)目中創(chuàng)建lib文件夾并放入 mysql-connector-java-8.0.21.jar

3.右鍵 mysql-connector-java-8.0.21.jar =>Build Path=>Add to Build Path

Java獲取Mysql數(shù)據(jù)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainTest {

    public static void main(String[] args) {
        // 聲明Connection對(duì)象
        Connection con;
        // 驅(qū)動(dòng)程序名
        String driver = "com.mysql.cj.jdbc.Driver";
        // URL指向要訪問的數(shù)據(jù)庫名 test
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false";
        // MySQL配置時(shí)的用戶名
        String user = "root";
        // MySQL配置時(shí)的密碼
        String password = "********";
        // 遍歷查詢結(jié)果集

            try {
                // 加載驅(qū)動(dòng)程序
                Class.forName(driver);//此驅(qū)動(dòng)程序在4.0版本后可不在加載 不加載也可正常使用
                //靜態(tài)代碼塊--->類加載,就執(zhí)行 為防二次注冊(cè) 推薦使用Class.forName加載驅(qū)動(dòng)
                //DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

                // 1.getConnection()方法,連接MySQL數(shù)據(jù)庫?。?/span>
                con = DriverManager.getConnection(url, user, password);

                if (!con.isClosed())//判斷是否成功連接
                   System.out.println("ntt成功以 " + user + " 身份連接到數(shù)據(jù)庫?。?!");

                // 2.創(chuàng)建statement類對(duì)象,用來執(zhí)行SQL語句?。?/span>
                Statement st = con.createStatement();

                // 要執(zhí)行的SQL語句
                String sql = "select * from author";
                // 3.ResultSet類,用來存放獲取的結(jié)果集??!
                ResultSet rs = st.executeQuery(sql);

                int ID = 0;
                String Name = null;
                String Sex = null;
                int Age = 0;

                System.out.println("ntt執(zhí)行結(jié)果如下所示:");
                System.out.println("tt-----------------------------------------------------------------");
                System.out.println("tt|t" + "ID" + "t" + "姓名" + "tt" + "性別" + "t" + "年齡t|");
                System.out.println("tt-----------------------------------------------------------------");

                while (rs.next()) {//4.遍歷查詢
                    // 獲取 ID 這列數(shù)據(jù)
                    ID = rs.getInt("id");
                    // 獲取 Name 這列數(shù)據(jù)
                    Name = rs.getString("author");
                    // 獲取 Sex 這列數(shù)據(jù)
                    Sex = rs.getString("sex");
                    // 獲取 Age 這列數(shù)據(jù)
                    Age = rs.getInt("age");
                    // 輸出結(jié)果
                    System.out.println("tt|t" + ID + "t" + Name + "tt" + Sex + "t" + Age + "t|tt");
                }

                System.out.println("tt-----------------------------------------------------------------");
                rs.close();//5.釋放資源
                st.close();
                con.close();
            }         
            catch (ClassNotFoundException e) {
                // 數(shù)據(jù)庫驅(qū)動(dòng)類異常處理
                System.out.println("Sorry,can`t find the Driver!");
                e.printStackTrace();
            }
            catch (SQLException e) {
                // 數(shù)據(jù)庫連接失敗異常處理
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                System.out.println("tttttttt獲取數(shù)據(jù)庫數(shù)據(jù)完畢!?。?);
            }
    }
}

運(yùn)行結(jié)果:
成功以 root 身份連接到數(shù)據(jù)庫?。?!

執(zhí)行結(jié)果如下所示:
-----------------------------------------------------------------
|    ID    姓名        性別    年齡    |
-----------------------------------------------------------------
|    1      yj         man      22        |        
|    2    迪麗熱巴     girl     18        |        
|    3    阿巴阿巴     pig       1        |        
-----------------------------------------------------------------
                    獲取數(shù)據(jù)庫數(shù)據(jù)完畢?。?!

優(yōu)化釋放資源 close

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUti {

    public static void main(String[] args) {
        // 聲明Connection對(duì)象
        Connection con = null;
        // 驅(qū)動(dòng)程序名
        String driver = "com.mysql.cj.jdbc.Driver";
        // URL指向要訪問的數(shù)據(jù)庫名 test
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false";
        // MySQL配置時(shí)的用戶名
        String user = "root";
        // MySQL配置時(shí)的密碼
        String password = "********";
        // 聲明Statement對(duì)象 用來執(zhí)行SQL語句
        Statement st = null;
        // 聲明ResultSet對(duì)象 ResultSet類,用來存放獲取的結(jié)果集?。?/span>
        ResultSet rs = null;
        // 遍歷查詢結(jié)果集
            try {
                // 加載驅(qū)動(dòng)程序
                Class.forName(driver);

                // 1.getConnection()方法,連接MySQL數(shù)據(jù)庫??!
                con = DriverManager.getConnection(url, user, password);

                if (!con.isClosed())//判斷是否成功連接
                   System.out.println("ntt成功以 " + user + " 身份連接到數(shù)據(jù)庫?。。?);

                // 2.創(chuàng)建statement類對(duì)象,用來執(zhí)行SQL語句??!
                st = con.createStatement();

                // 要執(zhí)行的SQL語句
                String sql = "select * from author";
                // 3.ResultSet類,用來存放獲取的結(jié)果集??!
                rs = st.executeQuery(sql);

                int ID = 0;
                String Name = null;
                String Sex = null;
                int Age = 0;

                System.out.println("ntt執(zhí)行結(jié)果如下所示:");
                System.out.println("tt-----------------------------------------------------------------");
                System.out.println("tt|t" + "ID" + "t" + "姓名" + "tt" + "性別" + "t" + "年齡t|");
                System.out.println("tt-----------------------------------------------------------------");

                while (rs.next()) {//4.遍歷查詢
                    // 獲取 ID 這列數(shù)據(jù)
                    ID = rs.getInt("id");
                    // 獲取 Name 這列數(shù)據(jù)
                    Name = rs.getString("author");
                    // 獲取 Sex 這列數(shù)據(jù)
                    Sex = rs.getString("sex");
                    // 獲取 Age 這列數(shù)據(jù)
                    Age = rs.getInt("age");
                    // 輸出結(jié)果
                    System.out.println("tt|t" + ID + "t" + Name + "tt" + Sex + "t" + Age + "t|tt");
                }

                System.out.println("tt-----------------------------------------------------------------");
            }         
            catch (ClassNotFoundException e) {
                // 數(shù)據(jù)庫驅(qū)動(dòng)類異常處理
                System.out.println("Sorry,can`t find the Driver!");
                e.printStackTrace();
            }
            catch (SQLException e) {
                // 數(shù)據(jù)庫連接失敗異常處理
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                //5.優(yōu)化釋放資源
                JDBCUtil.release(con, st, rs);
                System.out.println("tttttttt獲取數(shù)據(jù)庫數(shù)據(jù)完畢!??!");
            }
    }
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
 * 關(guān)閉的封裝
*/
public class JDBCUtil {

    public static void release(Connection con,Statement st,ResultSet rs) {
        closeRs(rs);
        closeSt(st);
        closeConn(con);
    }

    private static void closeRs(ResultSet rs) {
        try {
            if(rs != null) {
                rs.close();
            }
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        finally {
            rs = null;
        }
    }
    private static void closeSt(Statement st) {
        try {
            if(st != null) {
                st.close();
            }
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        finally {
            st = null;
        }
    }
    private static void closeConn(Connection con) {
        try {
            if(con != null) {
                con.close();
            }
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        finally {
            con = null;
        }
    }
}

優(yōu)化連接對(duì)象

import java.io.ObjectInputStream.GetField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class JDBC{
    public static Connection getCon() {//獲取連接對(duì)象
    // 聲明Connection對(duì)象
    Connection con = null;
    // 驅(qū)動(dòng)程序名
    String driver = "com.mysql.cj.jdbc.Driver";
    // URL指向要訪問的數(shù)據(jù)庫名 test
    String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false";
    // MySQL配置時(shí)的用戶名
    String user = "root";
    // MySQL配置時(shí)的密碼
    String password = "********";

    try {
        // 加載驅(qū)動(dòng)程序
        Class.forName(driver);

        // 1.getConnection()方法,連接MySQL數(shù)據(jù)庫?。?/span>
        con = DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return con;
 }
}


public class Demo1 {

    public static void main(String[] args) {
        // 聲明Connection對(duì)象
        Connection con = null;
        // 聲明Statement對(duì)象 用來執(zhí)行SQL語句
        Statement st = null;
        // 聲明ResultSet對(duì)象 ResultSet類,用來存放獲取的結(jié)果集?。?/span>
        ResultSet rs = null;
        // 遍歷查詢結(jié)果集
            try {
                con = JDBC.getCon();

                if (!con.isClosed())//判斷是否成功連接
                   System.out.println("ntt成功連接到數(shù)據(jù)庫?。。?);

                // 2.創(chuàng)建statement類對(duì)象,用來執(zhí)行SQL語句!!
                st = con.createStatement();

                // 要執(zhí)行的SQL語句
                String sql = "select * from author";
                // 3.ResultSet類,用來存放獲取的結(jié)果集??!
                rs = st.executeQuery(sql);

                int ID = 0;
                String Name = null;
                String Sex = null;
                int Age = 0;

                System.out.println("ntt執(zhí)行結(jié)果如下所示:");
                System.out.println("tt-----------------------------------------------------------------");
                System.out.println("tt|t" + "ID" + "t" + "姓名" + "tt" + "性別" + "t" + "年齡t|");
                System.out.println("tt-----------------------------------------------------------------");

                while (rs.next()) {//4.遍歷查詢
                    // 獲取 ID 這列數(shù)據(jù)
                    ID = rs.getInt("id");
                    // 獲取 Name 這列數(shù)據(jù)
                    Name = rs.getString("author");
                    // 獲取 Sex 這列數(shù)據(jù)
                    Sex = rs.getString("sex");
                    // 獲取 Age 這列數(shù)據(jù)
                    Age = rs.getInt("age");
                    // 輸出結(jié)果
                    System.out.println("tt|t" + ID + "t" + Name + "tt" + Sex + "t" + Age + "t|tt");
                }

                System.out.println("tt-----------------------------------------------------------------");
            }         
            catch (SQLException e) {
                // 數(shù)據(jù)庫連接失敗異常處理
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                //5.釋放資源
                JDBCUtil.release(con, st, rs);
                System.out.println("tttttttt獲取數(shù)據(jù)庫數(shù)據(jù)完畢!??!");
            }
    }
}

配置文件 properties

1.在src下聲明jdbc.properties文件

driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false
user=root
password=xxxxxxxx

2.在工具類中使用靜態(tài)代碼塊讀取屬性

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.test.JDBCUtil;
/*
*使用配置文件 
*/
class JDBC{
    // 聲明Connection對(duì)象
    static Connection con = null;
    // 驅(qū)動(dòng)程序名
    static String driver = null;
    // URL指向要訪問的數(shù)據(jù)庫名 test
    static String url = null;
    // MySQL配置時(shí)的用戶名
    static String user = null;
    // MySQL配置時(shí)的密碼
    static String password = null;
    static {
        try {
            //創(chuàng)建一個(gè)屬性配置對(duì)象
            Properties properties = new Properties();
            //InputStream is = new FileInputStream("jdbc.properties");//放項(xiàng)目根目錄下則可使用 如放在src下則報(bào)錯(cuò)未找到
            //使用類加載器,讀取src底下的資源文件
            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //導(dǎo)入輸入流
            properties.load(is);
            //讀取屬性
            driver = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static Connection getCon() {//獲取連接對(duì)象

    try {
        // 加載驅(qū)動(dòng)程序
        Class.forName(driver);

        // 1.getConnection()方法,連接MySQL數(shù)據(jù)庫?。?/span>
        con = DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return con;
 }
}

public class Demo2 {

    public static void main(String[] args) {
        // 聲明Connection對(duì)象
        Connection con = null;
        // 聲明Statement對(duì)象 用來執(zhí)行SQL語句
        Statement st = null;
        // 聲明ResultSet對(duì)象 ResultSet類,用來存放獲取的結(jié)果集!!
        ResultSet rs = null;
        // 遍歷查詢結(jié)果集
            try {
                con = JDBC.getCon();

                if (!con.isClosed())//判斷是否成功連接
                   System.out.println("ntt成功連接到數(shù)據(jù)庫?。?!");

                // 2.創(chuàng)建statement類對(duì)象,用來執(zhí)行SQL語句??!
                st = con.createStatement();

                // 要執(zhí)行的SQL語句
                String sql = "select * from author";
                // 3.ResultSet類,用來存放獲取的結(jié)果集??!
                rs = st.executeQuery(sql);

                int ID = 0;
                String Name = null;
                String Sex = null;
                int Age = 0;

                System.out.println("ntt執(zhí)行結(jié)果如下所示:");
                System.out.println("tt-----------------------------------------------------------------");
                System.out.println("tt|t" + "ID" + "t" + "姓名" + "tt" + "性別" + "t" + "年齡t|");
                System.out.println("tt-----------------------------------------------------------------");

                while (rs.next()) {//4.遍歷查詢
                    // 獲取 ID 這列數(shù)據(jù)
                    ID = rs.getInt("id");
                    // 獲取 Name 這列數(shù)據(jù)
                    Name = rs.getString("author");
                    // 獲取 Sex 這列數(shù)據(jù)
                    Sex = rs.getString("sex");
                    // 獲取 Age 這列數(shù)據(jù)
                    Age = rs.getInt("age");
                    // 輸出結(jié)果
                    System.out.println("tt|t" + ID + "t" + Name + "tt" + Sex + "t" + Age + "t|tt");
                }

                System.out.println("tt-----------------------------------------------------------------");
            }         
            catch (SQLException e) {
                // 數(shù)據(jù)庫連接失敗異常處理
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                //5.釋放資源
                JDBCUtil.release(con, st, rs);
                System.out.println("tttttttt獲取數(shù)據(jù)庫數(shù)據(jù)完畢!??!");
            }
    }
}

Mysql的CRUD

常見的MySQL語句命令
進(jìn)入mysql 命令行:mysql -uroot -p;
查看所有數(shù)據(jù)庫:show databases;
創(chuàng)建數(shù)據(jù)庫:create database niu charset utf8;
刪除數(shù)據(jù)庫:drop database niu;
選擇數(shù)據(jù)庫:use databases;
查看所有表:show tables;
查看創(chuàng)建數(shù)據(jù)庫的語句:show create database databasename;
查看創(chuàng)建表的語句:show create table tablename;
查看表結(jié)構(gòu):desc tablenmae;


庫操作
創(chuàng)建數(shù)據(jù)庫:create database shujukuba;
創(chuàng)建帶字符集的數(shù)據(jù)庫:create database mydb2 CHARACTER SET=utf8;
創(chuàng)建帶校驗(yàn)的數(shù)據(jù)庫:create database mydb3 CHARACTER SET=utf8 COLLATE utf8_general_ci;
顯示數(shù)據(jù)庫:show databases;
刪除數(shù)據(jù)庫:drop database shujukuba;
修改數(shù)據(jù)庫編碼:ALTER DATABASE shujukuba character set gb2312; 


表操作
創(chuàng)建數(shù)據(jù)庫表(創(chuàng)建一個(gè)表名為:employee,該表中含有id、name、sex、birthday、job字段):
create table employee
(
    id int,
    name varchar(40),
    sex  char(4),
    birthday date,
    job  varchar(100),
);
表中增加image字段:alter table employee add image blob;
修改job值,使其長度為60(原長度為1000):alter table employee modify job varchar(60);
刪除sex列:alter table employee drop sex;
表名改為user(原名employee):rename table employee to user;
修改表的字符集為utf-8:alter table user character set utf8;
列name修改為username:alter table user change column name username varchar(100);
刪除表:drop table user;


增刪改查實(shí)例
新建表employee:
create table employee
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
salary decimal(8,2),
resume text
);

插入數(shù)據(jù):
insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');
指定某些列插入數(shù)據(jù):insert into employee(id) values(6);
插入漢字:insert into employee(id,name) values(6,'張三');

修改表數(shù)據(jù):
將所有員工薪水修改為5000元:update employee set salary=5000;
將姓名為’zs’的員工薪水修改為3000元:update employee set salary = 3000 where name='zhangsan';
將姓名為’aaa’的員工薪水修改為4000元,job改為ccc:update employee set salary = 4000,job='ccc' where name='張三';
將wu的薪水在原有基礎(chǔ)上增加1000元:update employee set salary = salary+1000 where name='張三';

刪除表數(shù)據(jù):
刪除表中名稱為“zs”的記錄:delete from employee where job='ccc';
刪除表中所有記錄:delete from employee;
使用truncate刪除表中記錄:truncate table employee;

查詢表數(shù)據(jù):
查詢表中所有學(xué)生的信息:select id,name,chinese,english,math from student;
查詢表中所有學(xué)生的姓名和對(duì)應(yīng)的英語成績(jī):select name,english from student;
查詢姓名為wu的學(xué)生成績(jī):select * from student where name='張三';
查詢英語成績(jī)大于90分的同學(xué):select * from student where english>'90';
查詢英語分?jǐn)?shù)在 80-90之間的同學(xué):select * from student where english>=80 and english=<90;


常見MySQL字段含義
自增長:auto_increment
非空:not null
默認(rèn)值:default
唯一:unique
指定字符集:charset
主鍵:primary key

使用單元測(cè)試,測(cè)試代碼

1.定義一個(gè)類,TestXXXX,里面定義方法testXXX

2.添加junit的支持

右鍵工程 --- Build Path --- add Library --- Junit --- Junit4

3.在方法的上面加上注釋(標(biāo)記)

@Test
public void testQuery(){
    ...
}

4.光標(biāo)選中方法名字,然后右鍵執(zhí)行單元測(cè)試。或打開outline視圖,然后選擇方法右鍵執(zhí)行

插入

//核心代碼
    // 要執(zhí)行的SQL語句
    String sql = "insert into user(name,sex,birthday,entry_date,salary,resume) values('xx','man','199x-xx-xx','2020-7-11','1000','i am a developer')";
    // ResultSet類,存放獲取的結(jié)果集
    rs = st.executeQuery(sql);
    if(rs>0){
        System.out.println("添加成功");
    }else{
        System.out.println("添加失敗");
    }

刪除

//核心代碼
    // 要執(zhí)行的SQL語句
    String sql = "delete from user where id=2";
    // ResultSet類,存放獲取的結(jié)果集
    rs = st.executeQuery(sql);
    if(rs>0){
        System.out.println("刪除成功");
    }else{
        System.out.println("刪除失敗");
    }

更新

//核心代碼
    // 要執(zhí)行的SQL語句
    String sql = "update user set name='yj',birthday='1998-xx-xx' where id=1";
    // ResultSet類,存放獲取的結(jié)果集
    rs = st.executeQuery(sql);
    if(rs>0){
        System.out.println("刪除成功");
    }else{
        System.out.println("刪除失敗");
    }
//核心代碼
    // 要執(zhí)行的SQL語句
    String sql = "update user set name=?,birthday=? where id=?";

    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1,"姓名");//從左到右數(shù) 1代表第一個(gè)?
    ps.setString(2,"生日");//傳入什么類型 就set什么類型
    ps.setInt(3,1);
    rs = ps.executeQuery();

    if(rs>0){
        System.out.println("刪除成功");
    }else{
        System.out.println("刪除失敗");
    }
//封裝成通用方法
public static int executeDML(String sql,Object...objs){//sql為要執(zhí)行的mysql語句

    try {
        ps = con.prepareStatement(sql);
        for(int i=0;i<objs.length;i++){
            ps.setObject(i+1,objs[i]);
        }
        return ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } 

    return -1;
}

完結(jié)封裝

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.test.JDBCUtil;
class JDBC{
    // 聲明Connection對(duì)象
    static Connection con = null;
    // 驅(qū)動(dòng)程序名
    static String driver = null;
    // URL指向要訪問的數(shù)據(jù)庫名 test
    static String url = null;
    // MySQL配置時(shí)的用戶名
    static String user = null;
    // MySQL配置時(shí)的密碼
    static String password = null;
    static {
        try {
            //創(chuàng)建一個(gè)屬性配置對(duì)象
            Properties properties = new Properties();
            //InputStream is = new FileInputStream("jdbc.properties");//放項(xiàng)目根目錄下則可使用 如放在src下則報(bào)錯(cuò)未找到
            //使用類加載器,讀取src底下的資源文件
            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //導(dǎo)入輸入流
            properties.load(is);
            //讀取屬性
            driver = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getCon() {//獲取連接對(duì)象
    try {
        // 加載驅(qū)動(dòng)程序
        Class.forName(driver);//可省略

        // 1.getConnection()方法,連接MySQL數(shù)據(jù)庫!!
        con = DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return con;
 }
}


public class Demo3 {
    // 聲明Connection對(duì)象
    static Connection con = null;
    // 聲明Statement對(duì)象 用來執(zhí)行SQL語句
    static Statement st = null;
    // 聲明ResultSet對(duì)象 ResultSet類,用來存放獲取的結(jié)果集!!
    static ResultSet rs = null;
    //聲明PreparedStatement對(duì)象,用來針對(duì)?
    static PreparedStatement ps = null;

    public static int executeDML(String sql,Object...objs){//sql為要執(zhí)行的mysql語句
       //增刪改的封裝方法
        try {
            ps = con.prepareStatement(sql);
            for(int i=0;i<objs.length;i++){
                ps.setObject(i+1,objs[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } 

        return -1;
    }

    public static void main(String[] args) {

            try {
                con = JDBC.getCon();

                st = con.createStatement();

                // 要執(zhí)行的SQL語句
                String sql = "update user set name=?,birthday=? where id=?";
                int i = executeDML(sql, "蒼老師","2100-10-10",1);//此封裝方法可以將增 刪 改 合為一句

                if(i>0){
                    System.out.println("修改成功");
                }else{
                    System.out.println("修改失敗");
                }
            }         
            catch (SQLException e) {
                // 數(shù)據(jù)庫連接失敗異常處理
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                //5.釋放資源
                JDBCUtil.release(con, st, rs);
            }
    }
}
阿里企業(yè)郵箱、網(wǎng)易企業(yè)郵箱、新網(wǎng)企業(yè)郵箱
【標(biāo)準(zhǔn)版】400元/年/5用戶/無限容量
【外貿(mào)版】500元/年/5用戶/無限容量
其它服務(wù):網(wǎng)站建設(shè)、企業(yè)郵箱、數(shù)字證書ssl、400電話、
聯(lián)系方式:電話:13714666846 微信同號(hào)

聲明:本站所有作品(圖文、音視頻)均由用戶自行上傳分享,或互聯(lián)網(wǎng)相關(guān)知識(shí)整合,僅供網(wǎng)友學(xué)習(xí)交流,若您的權(quán)利被侵害,請(qǐng)聯(lián)系 管理員 刪除。

本文鏈接:http://www.goalq.com.cn/article_32813.html