JDBC_PreparedStatement

插入数据

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
        //加载配置文件
        InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
        Properties properties = new Properties();
        properties.load(is);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        //连接数据库
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,user,password);
        //插入语句
        String sql = "insert into emp values(default,?,?)"; //预编译语句
        PreparedStatement ps =conn.prepareStatement(sql);
        ps.setString(1,"admin1"); //设置参数
        ps.setString(2,"1234");     //设置参数
        ps.executeUpdate(); //执行
        //关闭资源
        ps.close();
        conn.close();
    }
原文地址:https://www.cnblogs.com/baisha/p/15463877.html