使用Statement执行DML和DQL语句

import com.loaderman.util.JdbcUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

/**
 * 使用Statement执行DML语句
 *
 */
public class Demo2 {
    private String url = "jdbc:mysql://localhost:3306/test";
    private String user = "root";
    private String password = "root";

    /**
     * 增加
     */
    @Test
    public void testInsert(){
        Connection conn = null;
        Statement stmt = null;
        try {
            //通过工具类获取连接对象
            conn = JdbcUtil.getConnection();
            
            //3.创建Statement对象
            stmt = conn.createStatement();
            
            //4.sql语句
            String sql = "INSERT INTO student(NAME,gender) VALUES('李四','女')";
            
            //5.执行sql
            int count = stmt.executeUpdate(sql);
            
            System.out.println("影响了"+count+"行");
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            /*if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }*/
            JdbcUtil.close(conn, stmt);
        }
    }
    
    /**
     * 修改
     */
    @Test
    public void testUpdate(){
        Connection conn = null;
        Statement stmt = null;
        //模拟用户输入
        String name = "陈六";
        int id = 3;
        try {
            /*//1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            
            //2.获取连接对象
            conn = DriverManager.getConnection(url, user, password);*/
            //通过工具类获取连接对象
            conn = JdbcUtil.getConnection();
            
            //3.创建Statement对象
            stmt = conn.createStatement();
            
            //4.sql语句
            String sql = "UPDATE student SET NAME='"+name+"' WHERE id="+id+"";
            
            System.out.println(sql);
            
            //5.执行sql
            int count = stmt.executeUpdate(sql);
            
            System.out.println("影响了"+count+"行");
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            /*if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }*/
            JdbcUtil.close(conn, stmt);
        }
    }
    
    /**
     * 删除
     */
    @Test
    public void testDelete(){
        Connection conn = null;
        Statement stmt = null;
        //模拟用户输入
        int id = 3;
        try {
            /*//1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            
            //2.获取连接对象
            conn = DriverManager.getConnection(url, user, password);*/
            //通过工具类获取连接对象
            conn = JdbcUtil.getConnection();
            
            //3.创建Statement对象
            stmt = conn.createStatement();
            
            //4.sql语句
            String sql = "DELETE FROM student WHERE id="+id+"";
            
            System.out.println(sql);
            
            //5.执行sql
            int count = stmt.executeUpdate(sql);
            
            System.out.println("影响了"+count+"行");
            
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //关闭资源
            /*if(stmt!=null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }*/
            JdbcUtil.close(conn, stmt);
        }
    }
}

import com.loaderman.util.JdbcUtil;

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

import org.junit.Test;

/**
 * 使用Statement执行DQL语句(查询操作)
 *
 */
public class Demo3 {

    @Test
    public void test1(){
        Connection conn = null;
        Statement stmt = null;
        try{
            //获取连接
            conn = JdbcUtil.getConnection();
            //创建Statement
            stmt = conn.createStatement();
            //准备sql
            String sql = "SELECT * FROM student";
            //执行sql
            ResultSet rs = stmt.executeQuery(sql);
            
            //移动光标
            /*boolean flag = rs.next();
            
            flag = rs.next();
            flag = rs.next();
            if(flag){
                //取出列值
                //索引
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String gender = rs.getString(3);
                System.out.println(id+","+name+","+gender);
                
                //列名称
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println(id+","+name+","+gender);
            }*/
            
            //遍历结果
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println(id+","+name+","+gender);
            }
            
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            JdbcUtil.close(conn, stmt);
        }
    }
}


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * jdbc工具类
 *
 */
public class JdbcUtil {
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static String driverClass = null;
    
    /**
     * 静态代码块中(只加载一次)
     */
    static{
        try {
            //读取db.properties文件
            Properties props = new Properties();
            /**
             *  . 代表java命令运行的目录
             *  在java项目下,. java命令的运行目录从项目的根目录开始
             *  在web项目下,  . java命令的而运行目录从tomcat/bin目录开始
             *  所以不能使用点.
             */
            //FileInputStream in = new FileInputStream("./src/db.properties");
            
            /**
             * 使用类路径的读取方式
             *  / : 斜杠表示classpath的根目录
             *     在java项目下,classpath的根目录从bin目录开始
             *     在web项目下,classpath的根目录从WEB-INF/classes目录开始
             */
            InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");
            
            //加载文件
            props.load(in);
            //读取信息
            url = props.getProperty("url");
            user = props.getProperty("user");
            password = props.getProperty("password");
            driverClass = props.getProperty("driverClass");
            
            
            //注册驱动程序
            Class.forName(driverClass);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("驱程程序注册出错");
        }
    }

    /**
     * 抽取获取连接对象的方法
     */
    public static Connection getConnection(){
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    
    /**
     * 释放资源的方法
     */
    public static void close(Connection conn,Statement stmt){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
    
    public static void close(Connection conn,Statement stmt,ResultSet rs){
        if(rs!=null)
            try {
                rs.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
                throw new RuntimeException(e1);
            }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
}

db.properties

url=jdbc:mysql://localhost:3306/test
user=root
password=root
driverClass=com.mysql.jdbc.Driver

猜你喜欢

转载自www.cnblogs.com/loaderman/p/10007430.html