zoukankan      html  css  js  c++  java
  • JDBC操作数据库的基本流程

    所有的JDBC应用程序都具有下面的基本流程:
      1、加载数据库驱动并建立到数据库的连接。
      2、执行SQL语句。
      3、处理结果。
      4、从数据库断开连接释放资源。

    下面我们就来仔细看一看每一个步骤:

    其实按照上面所说每个阶段都可得单独拿出来写成一个独立的类方法文件。共别的应用来调用。

    1、加载数据库驱动并建立到数据库的连接:

    1. String driverName="com.mysql.jdbc.Driver";  
    2. String connectiionString="jdbc:mysql://10.5.110.239:3306/test?"+"user=root&password=chen&characterEncoding=utf-8";  
    3. Connection connection=null;  
    4. try {  
    5.     Class.forName(driverName);//这里是所谓的数据库驱动的加载  
    6.     connection=(Connection) DriverManager.getConnection(connectiionString);//这里就是建立数据库连接  
    7.     System.out.println("数据库连接成功");  
    8. } catch (ClassNotFoundException e) {  
    9.     // TODO Auto-generated catch block  
    10.     e.printStackTrace();  
    11. }  
    12. return connection;  
    		String driverName="com.mysql.jdbc.Driver";
    		String connectiionString="jdbc:mysql://10.5.110.239:3306/test?"+"user=root&password=chen&characterEncoding=utf-8";
    		Connection connection=null;
    		try {
    			Class.forName(driverName);//这里是所谓的数据库驱动的加载
    			connection=(Connection) DriverManager.getConnection(connectiionString);//这里就是建立数据库连接
    			System.out.println("数据库连接成功");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return connection;

    2、执行SQL语句:

    在执行sql语句的时候,这里常见的有两种类型的语句对象:

    Statement:它提供了直接在数据库中执行SQL语句的方法。对于那些只执行一次的查询、删除或者一种固定的sql语句来说已经足够了。

    1. Statement statement=(Statement) dUtil.getConnection().createStatement();  
    2.               
    3.             String sql="delete from diary where title="+"'"+title+"'";  
    4.               
    5.             int count=statement.executeUpdate(sql);  
    6.               
    7.             System.out.println("删除成功");  
    Statement statement=(Statement) dUtil.getConnection().createStatement();
    			
    			String sql="delete from diary where title="+"'"+title+"'";
    			
    			int count=statement.executeUpdate(sql);
    			
    			System.out.println("删除成功");

    Preparedstatement:这种语句对象用于那些需要执行多次,每次仅仅是数据取值不同的SQL语句,它还提供了一些方法,以便指出语句所使用的输入参数。

    1. String sql="insert into diary(title,content,authorname,time) values(?,?,?,now())";  
    2.         try {  
    3.             PreparedStatement preparedStatement=(PreparedStatement) dUtil.getConnection().prepareStatement(sql);  
    4.             String title=diary.getTitle();  
    5.             String content=diary.getContent();  
    6.             String authorname=diary.getAuthorName();  
    7.             preparedStatement.setString(1, title);  
    8.             preparedStatement.setString(2, content);  
    9.             preparedStatement.setString(3, authorname);  
    String sql="insert into diary(title,content,authorname,time) values(?,?,?,now())";
    		try {
    			PreparedStatement preparedStatement=(PreparedStatement) dUtil.getConnection().prepareStatement(sql);
    			String title=diary.getTitle();
    			String content=diary.getContent();
    			String authorname=diary.getAuthorName();
    			preparedStatement.setString(1, title);
    			preparedStatement.setString(2, content);
    			preparedStatement.setString(3, authorname);

      3、处理结果:

    1. ResultSet resultSet=statement.executeQuery(sql);  
    2.             while (resultSet.next()) {  
    3.                 Diary diary=new Diary();  
    4.                 diary.setAuthorName(resultSet.getString("authorname"));  
    5.                 diary.setContent(resultSet.getString("content"));  
    6.                 diary.setTitle(resultSet.getString("title"));  
    7.                 diary.setId(resultSet.getInt("id"));  
    8.                 Date time=resultSet.getDate("time");  
    ResultSet resultSet=statement.executeQuery(sql);
    			while (resultSet.next()) {
    				Diary diary=new Diary();
    				diary.setAuthorName(resultSet.getString("authorname"));
    				diary.setContent(resultSet.getString("content"));
    				diary.setTitle(resultSet.getString("title"));
    				diary.setId(resultSet.getInt("id"));
    				Date time=resultSet.getDate("time");

    此处,应该知道的是:Statement执行sql语句的方法:insert、Update、delete语句是使用了Statement的executeUpdate方法执行的,返回结果是插入、更新、删除的个数。而select语句执行较为特别是使用了Statement的executeQuery方法执行的。返回的结果存放在resultset结果集中,我们可以调用next()方法来移到结果集中的下一条记录。结果集由行和列组成,各列数据可以通过相应数据库类型的一系列get方法(如getString,getInt,getDate等等)来取得。

    4、从数据库断开连接释放资源:

    在结果集、语句和连接对象用完以后,我们必须正确地关闭它们。连接对象、结果集对象以及所有的语句对象都有close()方法,通过调用这个方法,我们可以确保正确释放与特定数据库系统相关的所有资源。

    1. public static void closeConnection(ResultSet resultSet,PreparedStatement preparedStatement, Connection connection) throws SQLException {  
    2.           
    3.         if (resultSet!=null) resultSet.close();  
    4.         if (preparedStatement!=null) preparedStatement.close();  
    5.         if(connection!=null&&connection.isClosed()==false) connection.close();  
    6.         System.out.println("数据库关闭");  
    7.       
    8.     }  
    public static void closeConnection(ResultSet resultSet,PreparedStatement preparedStatement, Connection connection) throws SQLException {
    		
    		if (resultSet!=null) resultSet.close();
    		if (preparedStatement!=null) preparedStatement.close();
    		if(connection!=null&&connection.isClosed()==false) connection.close();
    		System.out.println("数据库关闭");
    	
    	}
  • 相关阅读:
    初学网络编程
    Python模块(三)(正则,re,模块与包)
    Python模块(二)(序列化)
    idea2020.2安装与破解
    部署-jenkins发布项目到linux环境
    部署-jenkins发布项目到windows环境
    部署-jenkins与gitlab结合使用
    部署-docker推送镜像到远程仓库
    部署-gitlab克隆地址踩坑
    部署-docker安装jenkins
  • 原文地址:https://www.cnblogs.com/heartstage/p/3416601.html
Copyright © 2011-2022 走看看