JDBC1:Statement和PreparedStatement

JDBC

1.什么是ORM

ORM:在Java对象和关系数据库之间建立某种映射,就可以实现直接存取Java对象,JavaBean

2.什么是JDBC

JDBC:Java Data Base Connectivity 是一个接口,可以执行SQL语句的JavaAPI

3.为什么要用JDBC

1.数据库中的数据是给谁用的

数据是给程序用的,而我们写的就是Java的程序,所以要用Java程序去连接数据库来访问数据

2.世面上有很多数据库,本来我们是需要根据不同的数据库学习不同的API,sun公司为了简化这个操作,提升程序员的幸福感,就定义了一套规范,就是JDBC API(接口)。对我们来说,使用不同的数据库时,只要用数据库厂商提供的数据库驱动程序即可。

4. 怎么做呢

步骤:
(1)导入MySQL的驱动包

(2)装载数据库驱动程序

(3)获取到与数据库的连接

(4)获取可以执行的SQL语句的对象

(5)执行SQL语句

(6)获取结果集--不是必须有,看需求

(7)关闭连接

4.1 第一种

使用Statement作为执行SQL语句的对象:

@Test
       public void getConnection1(){
           //
           Connection connection=null;
           Statement statement=null;
           ResultSet resultSet=null;

           try{
               //1.加载驱动
               //可以省略注册驱动DriverManager.registrDriver(new com.mysql.jdbc.Driver);
               //是因为Driver类里静态加载了注册驱动的步骤
//               static {
//                   try {
//                       DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//                   } catch (SQLException var1) {
//                       throw new RuntimeException("Can't register driver!");
//                   }
//               }
               Class.forName("com.mysql.jdbc.Driver");
               //2.获取数据库连接
               String url="jdbc:mysql://localhost:3306/myemployees?useSSL=false";
               String user="root";
               String password="419423mmzz";
               connection = DriverManager.getConnection(url, user, password);
               System.out.println(connection);
               //3.获取可以执行sql语句的对象Statement
               //Statement的弊端:(1)需要拼写sql语句(2)存在SQL注入问题
               //如何让避免出现SQL注入:用PreparedStatement(从Statement扩展而来),取代Statement
               statement=connection.createStatement();
               //4.获取结果集
               String sql="SELECT * FROM jobs";
               resultSet=statement.executeQuery(sql);
               //5.处理结果集
               while (resultSet.next()) {
                   String job_id=resultSet.getString(1);
                   String job_title=resultSet.getString(2);
                   String max_salary=resultSet.getString(3);
                   String min_salary = resultSet.getString(4);
                   System.out.println(job_id+","+job_title+" "+min_salary+" "+max_salary);
               }
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           } catch (SQLException e) {
               e.printStackTrace();
           }finally {
               //6.关闭连接
               if (resultSet != null) {
                   try {
                       resultSet.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
               if (statement != null) {
                   try {
                       statement.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
               if (connection != null) {
                   try {
                       connection.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
           }
       }
4.2 使用PreparedStatement替换Statement

一、为什么要进行替换呢

1、Statement对象编译SQL语句时,如果SQL语句有变量,就需要使用分隔符来隔开。如果变量很多,就会使得SQL变得非常复杂。PreparedStatement可以使用占位符,简化SQL的编写。

2.Statement会频繁编译SQL.PreparedStatement可对SQL进行预编译,提高效率,预编译的SQL存储在PreparedStatement对象中。

3.PreparedStatement防止SQL注入。(Statement通过分隔符‘++’,编写永等式,可以不需要密码就进入数据库)

public class Jdbc2 {
    @Test
    public void testInsert(){
        Connection connection=null;
        PreparedStatement ps=null;

        /**
        *这里的第一步,是JDBC连接数据库的第二种方式,通过读取配置文件jdbc.properties中的信息
        *来获取数据库连接必须的四个数据
        */
        //1.读取配置文件的信息
        InputStream is                  =Jdbc2.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros=new Properties();
        try {
            pros.load(is);
            String driver = pros.getProperty("driver");
            String url = pros.getProperty("url");
            String user = pros.getProperty("user");
            String password = pros.getProperty("password");

            //2.加载驱动
            Class.forName(driver);
            //3.获取数据库的连接
           connection = DriverManager.getConnection(url, user, password);
            //4.获取执行SQL语句的对象PrepareStatement
            //预编译sql语句
            String sql="insert into jobs(job_id,job_title,min_salary,max_salary) values(?,?,?,?)";
            ps = connection.prepareStatement(sql);
            //5.填充占位符
            ps.setString(1, "ZM");
            ps.setString(2, "IT");
            ps.setInt(3, 10000);
            ps.setInt(4,30000);
            //6.执行操作
            ps.execute();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.关闭资源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

jdbc.properties配置文件中的信息

//jdbc.properties配置文件中的信息
user=root
password=123
url=jdbc:mysql://localhost:3306/myemployees?useSSL=false
driver=com.mysql.jdbc.Driver

猜你喜欢

转载自blog.51cto.com/14234228/2484925