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();
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_38741971/article/details/80611967