1. 程式人生 > >JDBC學習(三、DDL、DML和DQL)

JDBC學習(三、DDL、DML和DQL)

 一、DDL操作

    我們來建立一張學生表,欄位我們給id,name,age,要求id主鍵,自增

程式碼演示:

package sql;

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

import org.junit.Test;

public class DDL {
	@Test
	public void createDemo(){
		String sql = "CREATE TABLE  t_student(id BIGINT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(20),age INT)";
		//宣告資源物件
		Connection conn = null;
		Statement stmt = null;
		
		//賈璉欲執事
		try {
			//1.載入註冊驅動
			Class.forName("com.mysql.jdbc.Driver");
			//2.獲取連線物件
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","rootroot");
			//3.獲取語句物件
			stmt = conn.createStatement();
			//4.執行SQL語句
			int rows = stmt.executeUpdate(sql);
			System.out.println(conn+"||"+sql+"||"+rows);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//5.釋放資源
			try {
				if (stmt!=null) {
					stmt.close();
				}
				if (conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}

程式碼執行效果:

 二、DML操作

    在t_student表中,插入,修改和刪除學生資訊.此時的操作模板和上述DDL一模一樣,僅僅只是SQL語句不一樣.

程式碼演示:

//DML
	@Test
	public void updateDemo(){
		String sql1 = "insert into t_student(name,age) values('dodo',23)";
		String sql2 = "update t_student set age=22 where id=1";
		String sql3 = "delete from t_student where id=1";
		//宣告資源物件
		Connection conn = null;
		Statement stmt = null;
		
		//賈璉欲執事
		try {
			//1.載入註冊驅動
			Class.forName("com.mysql.jdbc.Driver");
			//2.獲取連線物件
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","rootroot");
			//3.獲取語句物件
			stmt = conn.createStatement();
			//4.執行SQL語句
			int rows = stmt.executeUpdate(sql3);
			System.out.println(conn+"||"+sql3+"||"+rows);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//5.釋放資源
			try {
				if (stmt!=null) {
					stmt.close();
				}
				if (conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

 三、DQL操作

程式碼演示:

    //DQL
    @Test
    public void selectDemo(){
        String sql = "select * from t_student";
        //宣告資源物件
        Connection conn = null;
        Statement stmt = null;
        //宣告結果集物件
        ResultSet rs = null;
        //賈璉欲執事
        try {
            //1.載入註冊驅動
            Class.forName("com.mysql.jdbc.Driver");
            //2.獲取連線物件
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","rootroot");
            //3.獲取語句物件
            stmt = conn.createStatement();
            //4.執行SQL語句,建立結果集物件
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String name = rs.getString(2);
                int age = rs.getInt("age");
                System.out.println(name+","+age);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.釋放資源
            try {
                if (stmt!=null) {
                    stmt.close();
                }
                if (conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }