JDBC全称为:Java Data Base Connectivity,它是能够执行SQL语句的Java APIjava
步骤:mysql
Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { /* * 加载驱动有两种方式 * * 1:会致使驱动会注册两次,过分依赖于mysql的api,脱离的mysql的开发包,程序则没法编译 * 2:驱动只会加载一次,不须要依赖具体的驱动,灵活性高 * * 咱们通常都是使用第二种方式 * */ //1. //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //2. Class.forName("com.mysql.jdbc.Driver"); //获取与数据库链接的对象-Connetcion connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhongfucheng", "root", "root"); //获取执行sql语句的statement对象 statement = connection.createStatement(); //执行sql语句,拿到结果集 resultSet = statement.executeQuery("SELECT * FROM users"); //遍历结果集,获得数据 while (resultSet.next()) { System.out.println(resultSet.getString(1)); System.out.println(resultSet.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { /* * 关闭资源,后调用的先关闭 * * 关闭以前,要判断对象是否存在 * */ 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(); } } }
上面咱们已经简单使用JDBC去查询数据库的数据了,接下来咱们去了解一下上面代码用到的对象sql
客户端与数据库全部的交互都是经过Connection来完成的。数据库
经常使用的方法:api
//建立向数据库发送sql的statement对象。 createcreateStatement() //建立向数据库发送预编译sql的PrepareSatement对象。 prepareStatement(sql) //建立执行存储过程的callableStatement对象 prepareCall(sql) //设置事务自动提交 setAutoCommit(boolean autoCommit) //提交事务 commit() //回滚事务 rollback()
Statement对象用于向数据库发送Sql语句,对数据库的增删改查均可以经过此对象发送sql语句完成。markdown
Statement对象的经常使用方法:工具
//查询 executeQuery(String sql) //增删改 executeUpdate(String sql) //任意sql语句均可以,可是目标不明确,不多用 execute(String sql) //把多条的sql语句放进同一个批处理中 addBatch(String sql) //向数据库发送一批sql语句执行 executeBatch()
ResultSet对象表明Sql语句的执行结果,当Statement对象执行executeQuery()时,会返回一个ResultSet对象学习
ResultSet对象维护了一个数据行的游标【简单理解成指针】,调用ResultSet.next()方法,可让游标指向具体的数据行,进行获取该行的数据url
经常使用方法:spa
//获取任意类型的数据 getObject(String columnName) //获取指定类型的数据【各类类型,查看API】 getString(String columnName) //对结果集进行滚动查看的方法 next() Previous() absolute(int row) beforeFirst() afterLast()
经过上面的理解,咱们已经可以使用JDBC对数据库的数据进行增删改查了,咱们发现,不管增删改查都须要链接数据库,关闭资源,因此咱们把链接数据库,释放资源的操做抽取到一个工具类
/* * 链接数据库的driver,url,username,password经过配置文件来配置,能够增长灵活性 * 当咱们须要切换数据库的时候,只须要在配置文件中改以上的信息便可 * * */ private static String driver = null; private static String url = null; private static String username = null; private static String password = null; static { try { //获取配置文件的读入流 InputStream inputStream = UtilsDemo.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取配置文件的信息 driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //加载驱动类 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,username,password); } public static void release(Connection connection, Statement statement, ResultSet resultSet) { 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(); } } }