• jdbc简单应用


    jdbc简单应用

    1、jdbc概述

    Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。

    2、jdbc的好处

    jdbc提供标准操作数据库的api,不管使用那些数据库,只要应用层使用jdbc标准的api,数据库 切换或者移植,针对上层基础代确没有影响,因此具有操作数据库代码的可移植性

    3、如何使用jdbc

    3.1、jdbc核心类

    • DriverManager:用于管理JDBC驱动的服务类

    • Connection:代表数据库连接对像,每一个Connection代表一个物理连接会话

    • Statement:用于执行SQL语句的工具接口

    • PreparedStatement:预编译的Statement对象

    • ResultSet:结果集对象

    3.2、代码体验

    public class Jdbc1 {
       private static final String USERNAME="root";//数据库用户名
       private static final String PASSWORD="123456";//数据库密码
       //数据库驱动(mysql5之后要加cj)
       private static final String DRIVER="com.mysql.cj.jdbc.Driver";
       //数据库连接地址(针对mysq1地址写法: jdbc :mysql://主机ip:3306/数据库名)
       private static final String URL="jdbc:mysql://localhost:3306/homework?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

       static {
           try {
               //加载驱动(利用了反射机制),如果没有导入驱动jar包,则抛异常
               Class.forName(DRIVER);
          } catch (ClassNotFoundException e) {
               e.printStackTrace();
          }
      }

       /**
        * 获取连接
        */
       public static Connection getConnection() throws SQLException {
           return DriverManager.getConnection(URL,USERNAME,PASSWORD);
      }

       /**
        * 关闭连接
        */
       public static void close(Connection con,PreparedStatement pre,ResultSet res) {
           try {
               con.close();
               pre.close();
               res.close();
          }catch (Exception e) {
               e.printStackTrace();
          }
      }

       /**
        * 关闭连接
        */
       public static void close(Connection con,PreparedStatement pre) {
           try {
               con.close();
               pre.close();
          }catch (Exception e) {
               e.printStackTrace();
          }
      }

       /*
        *添加数据
        */
       public void add(){
           try {
               //获取连接
               Connection connection=getConnection();
               //sql语句
               String sql="insert into teacher_table values(?,?)";
               //预编译
               PreparedStatement preparedStatement=connection.prepareStatement(sql);
               //设置参数
               preparedStatement.setObject(1,4);
               preparedStatement.setObject(2,"胡图图");
               //执行sql
               int lineNumber=preparedStatement.executeUpdate();
               //判断是否插入成功
               if (lineNumber>0){
                   System.out.println("添加成功");
              }else {
                   System.out.println("添加失败");
              }
               //关闭连接
               close(connection,preparedStatement);
          } catch (SQLException throwables) {
               throwables.printStackTrace();
          }
      }

       /**
        *删除数据
        */
       public void delete(){
           try {
           //获取连接
           Connection connection=getConnection();
           //sql语句
           String sql="DELETE FROM teacher_table WHERE teacher_id=?";
           //预编译
           PreparedStatement preparedStatement=connection.prepareStatement(sql);
           //设置参数
           preparedStatement.setObject(1,4);
           //执行sql
           int lineNumber=preparedStatement.executeUpdate();
           //判断是否插入成功
           if (lineNumber>0){
               System.out.println("删除成功");
          }else {
               System.out.println("删除失败");
          }
           //关闭连接
           close(connection,preparedStatement);
      } catch (SQLException throwables) {
           throwables.printStackTrace();
          }
      }

       /**
        *查询数据
        */
       public void query(){
           try {
               //获取连接
               Connection connection=getConnection();
               //sql
               String sql="SELECT * FROM teacher_table WHERE teacher_id=?";
               //预编译sql
               PreparedStatement prepareStatement=connection.prepareStatement(sql);
               //设置参数
               prepareStatement.setObject(1,1);
               //执行sql,得到结果集
               ResultSet resultSet=prepareStatement.executeQuery();
               //遍历结果集
               while (resultSet.next()){
                   //获取结果并输出
                   int id=resultSet.getInt(1);
                   String name=resultSet.getString(2);
                   System.out.println(id+" "+name);
              }
               //关闭连接
               close(connection,prepareStatement,resultSet);
          } catch (SQLException throwables) {
               throwables.printStackTrace();
          }
      }

       /**
        *修改数据
        */
       public void update(){
           try {
               //获取连接
               Connection connection=getConnection();
               //sql
               String sql="update teacher_table set teacher_name=? where teacher_id=?";
               //预编译sql
               PreparedStatement prepareStatement=connection.prepareStatement(sql);
               //设置参数
               prepareStatement.setObject(1,"糊涂图");
               prepareStatement.setObject(2,1);
               //执行sql
               int lineNumber=prepareStatement.executeUpdate();
               //判断是否修改成功
               if (lineNumber>0){
                   System.out.println("修改成功");
              }else {
                   System.out.println("修改失败");
              }
               //关闭连接
               close(connection,prepareStatement);
          } catch (SQLException throwables) {
               throwables.printStackTrace();
          }
      }


       public static void main(String[] args) throws SQLException {
           Jdbc1 jdbc1=new Jdbc1();
           //添加数据
    //       jdbc1.add();
           //删除数据
    //       jdbc1.delete();
           //查询数据
           jdbc1.query();
           //修改数据
    //       jdbc1.update();
      }

    }

    4、使用PreparedStatement的好处

    参考资料

    5、jdbc的事务支持

    参考资料https://www.cnblogs.com/waka401/archive/2012/10/12/2721196.html

    记得快乐
  • 相关阅读:
    codesmith+mysql生成代码
    遭遇笔试
    线性是一种简洁,简洁就是美
    Microsoft Kinect SDK vs PrimeSense OpenNI
    资料收集:让OpenCV使用IPP
    提纲
    在PC上安装使用Kinect
    OpenNI设置Kinect帧率,读取IR图
    cout,rather than printf
    单步调试时,getnextframe会失败。又
  • 原文地址:https://www.cnblogs.com/Y-wee/p/13515880.html
Copyright © 2020-2023  润新知