中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

通過JDBC對(duì)MySql進(jìn)行增/刪/改/查操作

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
新建BaseDao
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
  
public class BaseDao {  
    Connection con = null;  
    Statement st = null;  
    ResultSet rs = null;  
  
    /** 
     * 獲得聯(lián)接 
     *  
     * @return 
     */  
    public Connection getConnection() {  
        try {  
            // 加載驅(qū)動(dòng),這一句也可寫為:Class.forName("com.mysql.jdbc.Driver");   
            Class.forName("com.mysql.jdbc.Driver").newInstance();  
  
            // 建立到MySQL的連接   
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/money_note?characterEncoding=UTF-8", "root", "root");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return con;  
    }  
  
    /** 
     * 關(guān)閉數(shù)據(jù)源 
     */  
    public void CloseConnection(Connection con, Statement s, ResultSet rs) {  
        try {  
            if (rs != null) {  
                rs.close();  
            }  
            if (s != null) {  
                s.close();  
            }  
            if (con != null) {  
                con.close();  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}  

測(cè)試類
import java.sql.Connection;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
  
  
public class Test extends BaseDao {  
  
    Connection con = null;  
    Statement st = null;  
    ResultSet rs = null;  
  
    /** 
     * 查詢數(shù)據(jù) 
     */  
    public void find() {  
        con = getConnection();  // 獲得聯(lián)接  
        try {  
            st = con.createStatement();  
            rs = st.executeQuery("select * from app_user");  
            while (rs.next()) {  
                System.out.println("編號(hào):" + rs.getInt("uuid") + ", 姓名:" + rs.getString("userName") + ", 性別:" + rs.getString("sex") + ", 生日:" + rs.getString("birthday") + ", 住址:" + rs.getString("address"));  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            CloseConnection(con, st, rs);   // 關(guān)閉聯(lián)接  
        }  
    }  
  
    /** 
     * 添加數(shù)據(jù) 
     */  
    public void add() {  
        con = getConnection();  
        try {  
            st = con.createStatement();  
            int result = st.executeUpdate("insert into app_user(userName,passWord,sex,birthday,address) values('趙麗穎','wanying','女','1992-02-03','北京市')");  
            if (result > 0) {  
                System.out.println("插入成功");  
            } else {  
                System.out.println("插入失敗");  
            }  
        } catch (SQLException e) {  
            System.out.println("插入失敗");  
            e.printStackTrace();  
        } finally {  
            CloseConnection(con, st, rs);  
        }  
    }  
  
    /** 
     * 更新數(shù)據(jù) 
     */  
    public void update() {  
        con = getConnection();  
        try {  
            st = con.createStatement();  
            int result = st.executeUpdate("update app_user set address = '河南' where uuid = '3'");  
            if (result > 0) {  
                System.out.println("更新成功");  
            } else {  
                System.out.println("更新失敗");  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            CloseConnection(con, st, rs);  
        }  
    }  
  
    /** 
     * 刪除數(shù)據(jù) 
     */  
    public void delete() {  
        con = getConnection();  
        try {  
            st = con.createStatement();  
            int result = st.executeUpdate("delete from app_user where uuid = '3'");  
            if (result > 0) {  
                System.out.println("刪除成功");  
            } else {  
                System.out.println("刪除失敗");  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            CloseConnection(con, st, rs);  
        }  
    }  
  
    public static void main(String[] args) {  
        Test test = new Test();  
        //test.add();  
        // test.update();  
         test.delete();  
        test.find();  
  
    }  
  
}  

標(biāo)簽: Mysql

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:jQuery實(shí)現(xiàn)返回頂部功能

下一篇:PHP生成自定義驗(yàn)證碼