使用prepareStatement来实现对数据库的插入操作

1、使用prepareStatement
2、数据库的字段中有String和Date类型
3、使用Properties来读取文件中的key-value
4、占位符?的使用
代码:

package test_video_kang.test01;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class PreparedStatementTest {
     

    @Test
    public void test01()  {
     
        Connection connection = null;
        PreparedStatement ps = null;

        try {
     
            //1、获取connection连接对象
            InputStream is = PreparedStatementTest.class.getClassLoader().getResourceAsStream("jdbc");
            Properties properties = new Properties();
            properties.load(is);
            String driverClass = properties.getProperty("driverClass");
            Class.forName(driverClass);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            connection = DriverManager.getConnection(url, user, password);

            //2、开始操作数据库
            //?叫做占位符,使用prepareStatement,使用占位符来写sql语句
            String sql="insert into t_user(uname,upwd,birth) values(?,?,?)";
            //预编译sql语句,返回PreparedStatement对象
            ps = connection.prepareStatement(sql);
            //填充占位符实际数据
            ps.setString(1,"宋红康");
            ps.setString(2,"123");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse("1000-01-01");
            ps.setDate(3,new java.sql.Date(date.getTime()));
            //执行sql操作
            ps.execute();
        } catch (IOException e) {
     
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
     
            e.printStackTrace();
        } catch (SQLException e) {
     
            e.printStackTrace();
        } catch (ParseException e) {
     
            e.printStackTrace();
        } finally {
     
            //资源的关闭
            try {
     
                if (ps!=null)
                     ps.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
            try {
     
                if (connection!=null)
                    connection.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
        }
    }

}

使用prepareStatement来实现对数据库的插入操作_第1张图片
疑难点补充:
使用prepareStatement来实现对数据库的插入操作_第2张图片

因为Mysql中,表t_user里面有一个字段是Date类型的,而且sql语句使用的是占位符,我们在setDate中必须传输一个java.sql包下的Date对象
java里面的Date是java.util包下的
MySQL中的Date是java.sql包下的
需要使用SimpleDateFormate作为转换

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = sdf.parse(“1000-01-01”);
ps.setDate(3,new java.sql.Date(date.getTime()));
最后补充一下:
代码的书写过程(尤其是指关闭资源的代码书写顺序)
先从上往下写代码一直到资源的关闭,一旦碰到编译型异常,那就throws Exception,最后再在idea中选中那些需要try catch的代码块,快捷键是:ctrl+alt+T

你可能感兴趣的:(mysql+java+jdbc,mysql)