当前位置: 首页 > news >正文

怎么做网站播放器wordpress 弹出框

怎么做网站播放器,wordpress 弹出框,网页设计有什么证书,简约装修大全JDBC和连接池 大纲 JDBC连接数据库的方式JDBCUtils事务 具体案例 JDBC 需求#xff1a;满足Java程序能对多个不同的数据库进行操作#xff0c;而创建了一种接口#xff0c;实现对数据库的规范 连接数据库的方式 1.方法1 先创建一个Driver对象#xff0c;然后设置…JDBC和连接池 大纲 JDBC连接数据库的方式JDBCUtils事务 具体案例 JDBC 需求满足Java程序能对多个不同的数据库进行操作而创建了一种接口实现对数据库的规范 连接数据库的方式 1.方法1 先创建一个Driver对象然后设置连接到的数据库的地址然后创建一个properties对象在里面设定好账户密码然后通过driver的connect方法创建出connect连接 public class jdbc01 {public static void main(String[] args) throws SQLException {// 前置工作在项目下创建文件夹然后将jar文件拷贝到该目录下// 然后将其加入到项目中// 1.注册驱动Driver driver new Driver();// 2.得到连接// (1)jdbc:mysql://表示表示规定好的协议// (2)localhost 应该是ip地址(这里是主机的ip地址)// (3)3306表示MySQL监听的端口// (4)test db 是指连接到MySQL的哪个数据库// (5)本质上是进行socket连接String url jdbc:mysql://localhost:3306/test01;// 将用户名和密码封装到一个Properties对象中Properties properties new Properties();// user和password是规定好的后面的值根据实际情况properties.setProperty(user,root);properties.setProperty(password, );Connection connect driver.connect(url, properties);// 3.执行sql语句String sql insert into actor values(null,刘德华,男,1970-11-11,110);// Statement 用于执行静态sql语句并返回生成的结果的对象Statement statement connect.createStatement();int rows statement.executeUpdate(sql);// 如果是dml语句返回的就是影响到的行数System.out.println(rows 0? 成功:失败);//4.关闭连接statement.close();connect.close();} }缺点driver是第三方的依赖性强灵活性差 2.使用反射机制 public class jdbc02 {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {Class? aClass Class.forName(com.mysql.jdbc.Driver);Driver driver (Driver) aClass.newInstance();String url jdbc:mysql://localhost:3306/test01;Properties properties new Properties();properties.setProperty(user,root);properties.setProperty(password,);Connection connect driver.connect(url, properties);System.out.println(connect);} }3.使用DriverManager替换Driver 这种方法具有更好的拓展性 public class jdbc03 {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {Class? aClass Class.forName(com.mysql.jdbc.Driver);Driver driver (Driver) aClass.newInstance();String url jdbc:mysql://localhost:3306/test01;String user root;String password ;// 也可以还是使用properties来存储账户和密码最后在DriverManager的getConnection方法里传入url和propertiesDriverManager.registerDriver(driver);Connection connection DriverManager.getConnection(url, user, password);System.out.println(connection);} } 4.自动注册简化操作(推荐使用) 在反射时完成了类的加载在静态代码块里实现了自动注册 public class jdbc04 {public static void main(String[] args) throws ClassNotFoundException, SQLException {Class.forName(com.mysql.jdbc.Driver);// 可以不写String url jdbc:mysql://localhost:3306/test01;String user root;String password lei2483034010;Connection connection DriverManager.getConnection(url, user, password);System.out.println(connection);} } 5.使用配置文件(最推荐) 在4方法的基础上使用配置文件来存储账户和密码更加的灵活 public class jdbc05 {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));String user properties.getProperty(user);String password properties.getProperty(password);String driver properties.getProperty(driver);String url properties.getProperty(url);Class.forName(com.mysql.jdbc.Driver);Connection connection DriverManager.getConnection(url, user, password);System.out.println(connection);} } 执行sql语句 实际开发中基本不使用statement因为它不能预防sql注入 所以使用preparedStarement来防止sql的注入 使用这个类的好处 public class PreparedStatement {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {Scanner myScanner new Scanner(System.in);System.out.println(请输入账号);String account myScanner.nextLine();System.out.println(请输入密码);String pwd myScanner.nextLine();Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));String user properties.getProperty(user);String password properties.getProperty(password);String driver properties.getProperty(driver);String url properties.getProperty(url);Class.forName(com.mysql.jdbc.Driver);Connection connection DriverManager.getConnection(url, user, password);String sqlSelect select name,pwd from admin where name ? and pwd ?;java.sql.PreparedStatement preparedStatement connection.prepareStatement(sqlSelect);// 赋值preparedStatement.setString(1,account);preparedStatement.setString(2,pwd);ResultSet resultSet preparedStatement.executeQuery();// 得到一个查询到resultSet集if (resultSet.next()){System.out.println(恭喜,登录成功);}else {System.out.println(对不起登录失败);}resultSet.close();preparedStatement.close();connection.close();} } JDBCUtils 把JDBC的连接数据库操作和关闭资源封装到一个工具类中 public class JDBCUtils {//定义相关的属性(4个), 因为只需要一份因此我们做出staticprivate static String user; //用户名private static String password; //密码private static String url; //urlprivate static String driver; //驱动名//在static代码块去初始化static {try {Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));//读取相关的属性值user properties.getProperty(user);password properties.getProperty(password);url properties.getProperty(url);driver properties.getProperty(driver);} catch (IOException e) {//在实际开发中我们可以这样处理//1. 将编译异常转成 运行异常//2. 调用者可以选择捕获该异常也可以选择默认处理该异常比较方便.throw new RuntimeException(e);}}//连接数据库, 返回Connectionpublic static Connection getConnection() {try {return DriverManager.getConnection(url, user, password);} catch (SQLException e) {//1. 将编译异常转成 运行异常//2. 调用者可以选择捕获该异常也可以选择默认处理该异常比较方便.throw new RuntimeException(e);}}//关闭相关资源/*1. ResultSet 结果集2. Statement 或者 PreparedStatement3. Connection4. 如果需要关闭资源就传入对象否则传入 null*/public static void close(ResultSet set, Statement statement, Connection connection) {//判断是否为nulltry {if (set ! null) {set.close();}if (statement ! null) {statement.close();}if (connection ! null) {connection.close();}} catch (SQLException e) {//将编译异常转成运行异常抛出throw new RuntimeException(e);}}} 事务Java中使用 public class Transaction_ {//没有使用事务.Testpublic void noTransaction() {//操作转账的业务//1. 得到连接Connection connection null;//2. 组织一个sqlString sql update account set balance balance - 100 where id 1;String sql2 update account set balance balance 100 where id 2;PreparedStatement preparedStatement null;//3. 创建PreparedStatement 对象try {connection JDBCUtils.getConnection(); // 在默认情况下connection是默认自动提交preparedStatement connection.prepareStatement(sql);preparedStatement.executeUpdate(); // 执行第1条sqlint i 1 / 0; //抛出异常preparedStatement connection.prepareStatement(sql2);preparedStatement.executeUpdate(); // 执行第3条sql} catch (SQLException e) {e.printStackTrace();} finally {//关闭资源JDBCUtils.close(null, preparedStatement, connection);}}//事务来解决Testpublic void useTransaction() {//操作转账的业务//1. 得到连接Connection connection null;//2. 组织一个sqlString sql update account set balance balance - 100 where id 1;String sql2 update account set balance balance 100 where id 2;PreparedStatement preparedStatement null;//3. 创建PreparedStatement 对象try {connection JDBCUtils.getConnection(); // 在默认情况下connection是默认自动提交//将 connection 设置为不自动提交connection.setAutoCommit(false); //开启了事务preparedStatement connection.prepareStatement(sql);preparedStatement.executeUpdate(); // 执行第1条sqlint i 1 / 0; //抛出异常preparedStatement connection.prepareStatement(sql2);preparedStatement.executeUpdate(); // 执行第3条sql//这里提交事务connection.commit();} catch (SQLException e) {//这里我们可以进行回滚即撤销执行的SQL//默认回滚到事务开始的状态.System.out.println(执行发生了异常撤销执行的sql);try {connection.rollback();} catch (SQLException throwables) {throwables.printStackTrace();}e.printStackTrace();} finally {//关闭资源JDBCUtils.close(null, preparedStatement, connection);}} }
http://www.pierceye.com/news/917539/

相关文章:

  • 自已建网站微信登录珠海绿网科技有限公司
  • 大良网站制作太原建筑公司网站
  • 网站开发的交付文档抖音代运营费用明细
  • 自适应网站建设沈阳网站安全建设需求
  • 列表主题wordpress国外seo综合查询
  • 装修网站怎么做推广做百度网站每年的费用多少
  • 网站搭建免费视频教程省企联网站建设要求
  • 天津大学生专业做网站建设网站价格
  • 携程网站建设进度及实施过程文具电子商务网站开发内容
  • 怎么查看网站打开速度网站源码整站下载
  • 北京城乡住房建设部网站常见的网络营销推广方式有哪些
  • 做网站的成本费用钱宝网站怎么做任务
  • 网站上的格式用html怎么做部队网站设计
  • 帮客户做网站内容社交网站有哪些如何做
  • 网站开发与设计实训总结两千字公众号制作的网站开发
  • 一个公司做2个产品网站怎么做的用html5做的网站素材
  • 内乡网站建设咸阳网站建设报价
  • 企业网站多少钱扶余手机网站开发
  • 做外汇网站卖判刑多少年如何找回网站后台密码
  • 怎么做优惠券网站asp.net mvc 5网站开发之美
  • 网站底部浮动电话广告福建住房和城乡建设部网站
  • 建站之星破解版wordpress 置顶排序
  • c2c网站代表和网址涟源市建设局网站
  • 哪个网站有免费的模板免费网上商城系统
  • 一个网站的建设需要什么东西前十强排名家装公司
  • 广州网站建设报价表石家庄搜索排名提升
  • 网站备案步骤企业网站手机版模板免费下载
  • 郑州高端品牌网站建设镇江网站营销推广
  • 网站开发简单的框架南昌手机网站
  • 网站分析与优化百度新闻源网站有哪些