实训笔记6.28

6.28

一、座右铭

我的故事你说,我的文字我落,我值几两你定,我去何方我挑。

二、Java编程语言操作数据库的技术

Java提供了一个技术:JDBC(Java database connection),JDBC代码都位于Java的java.sql包下,是Java专门用来提供的操作各种各样数据库的工具java.sql包下提供的JDBC技术大部分都是借口interface,接口只是告诉我们如果你要连接数据库,你应该怎么做,每一个方法代表什么含义,每一个接口基本上都没有实现类。

每个接口的实现类——每种数据库的具体如果使用JDBC连接,每一步方法如何操作,Java需要让数据库的服务厂商给我们提供,一般情况下,服务器厂商写好之后,都是给我们开发者提供了一个jar包,jar包当中封装了连接对应数据库底层的实现。

2.1 JDBC如何操作数据库

预备环节:引入施工队——导包——数据库服务器厂商给我们提供的JDBC的实现包

  1. 选址,加载驱动
  2. 创建桥梁,建立连接
  3. 准备物资,在Java中创建一个SQL语句
  4. 准备小推车——必须根据桥来创建
  5. 小推车带着物资去数据库进行执行
  6. 得到返回的结果,进行处理
  7. 毁车炸桥

代码示例:

package com.sxuek.study;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC操作数据库
 * @author 11018
 *
 */
public class Demo {
    
    
	public static void main(String[] args) {
    
    
	
		/**
		 * mysql :  com.mysql.jdbc.Driver  com.mysql.cj.jdbc.Driver
		 * oracle:  oracle.jdbc.driver.OracleDriver
		 */
		Connection connection = null;
		Statement statement = null;
		try {
    
    
			/**
			 *1、选址--加载驱动---第一步可以不用写
			 */
			Class.forName("com.mysql.cj.jdbc.Driver");
			/**
			 * 2、创建和数据库之间的连接
			 *   最少需要三个要素:
			 *       url:数据库的连接地址,不同的数据库的URL写法是不一样的
			 *         mysql:  jdbc:mysql://ip:port/database_name?key=value&key=value...
			 *                 serverTimezone=UTC
			 *                 useUnicode=true
			 *                 characterEncoding=UTF-8
			 *                 
			 *       username:数据库的用户名
			 *       password:数据库的密码
			 */
			connection = DriverManager.getConnection("jdbc:mysql://localhost:33006/school?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8", "root", "123456");
			/**
			 * 3、准备SQL语句
			 *    DML
			 *    DQL
			 *    TCL
			 */
			String sql ="insert into student(student_name,student_age,student_sno,student_phone,student_department_id) values('zs',20,'sno','15788888888',2)";
			
			/**
			 * 4、准备一个小推车:JDBC当中有两种小推车
			 *     Statement--不能防止SQL注入
			 *     PrepareStatement--可以预防SQL注入
			 */
			statement = connection.createStatement();
			/**
			 * 5、小推车带着SQL语句去数据库执行
			 *    DML:statement.executeUpdate(DMLSQL):int
			 *    DQL: statement.executeQuery(DQLSQL):ResultSet
			 *    DDL: statment.execute(DDLsql):boolean
			 */
			int result = statement.executeUpdate(sql);
			/**
			 * 6、处理返回结果
			 */
			if(result > 0) {
    
    
				System.out.println("执行成功,数据库受影响了"+result+"行");
			}else {
    
    
				System.out.println("服务器内部异常,请稍后重试或者联系管理员");
			}
		} catch (ClassNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			/**
			 * 7、关闭资源  三个资源
			 *    Connection
			 *    Statement
			 *    ResultSet
			 *    先创建的后关闭
			 */
			if(statement != null) {
    
    
				try {
    
    
					statement.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(connection != null) {
    
    
				try {
    
    
					connection.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

package com.sxuek.study;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 通过JDBC实现数据的查询
 * @author 11018
 *
 */
public class Demo01 {
    
    
	public static void main(String[] args) {
    
    
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
    
    
			//1、加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2、创建和数据库之间的连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:33006/school?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8","root","123456");
			//3、准备查询语句
			String sql = "select * from dept where department_id=1";
			//4、准备小推车
			statement = connection.createStatement();
			//5、执行SQL语句得到返回结果
			resultSet = statement.executeQuery(sql);
			/**
			 * 6、处理返回结果--获取结果集中所有虚拟表格的数据
			 */
			while(resultSet.next()) {
    
    
				//获取当前行的数据
				int departmentId = resultSet.getInt("department_id");
				String departmentName = resultSet.getString("department_name");
				System.out.println(departmentId+departmentName);
			}
			
		} catch (ClassNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			//7、关闭资源
			if(resultSet != null) {
    
    
				try {
    
    
					resultSet.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(statement != null) {
    
    
				try {
    
    
					statement.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
    
    
				try {
    
    
					connection.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

package com.sxuek.study;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

/**
 * JDBC两种小推车的区别
 *   Statement:不能防止SQL注入
 *      statement在执行SQL语句前,是带着参数一起去编译的,因此如果参数中出现了SQL中一些特殊字符,会被识别为SQL语言的一部分
 *   PreparedStatement:可以预防SQL注入
 *      预编译小推车,执行SQL前,如果SQL中需要传递参数,那么先使用占位符? 把SQL语言编译了,后期执行的时候传入参数,所有的参数全部当作
 *      普通的数据处理 而非SQL的特殊字符处理。
 * @author 11018
 *
 */
public class Demo02 {
    
    
	public static void main(String[] args) {
    
    
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
    
    
			String username = JOptionPane.showInputDialog("请输入用户名");
			String password = JOptionPane.showInputDialog("请输入密码");
			
			//1、加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2、创建和数据库之间的连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:33006/school?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8","root","123456");
			//3、准备查询语句
			String sql = "select * from user where user_name='"+username+"' and password='"+password+"'";
			System.out.println(sql);
			//4、准备小推车
			statement = connection.createStatement();
			//5、执行SQL语句得到返回结果
			resultSet = statement.executeQuery(sql);
			/**
			 * 6、处理返回结果--获取结果集中所有虚拟表格的数据
			 */
			while(resultSet.next()) {
    
    
				//获取当前行的数据
				String user = resultSet.getString("user_name");
				String pass = resultSet.getString("password");
				System.out.println(user+pass);
			}
			
		} catch (ClassNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			//7、关闭资源
			if(resultSet != null) {
    
    
				try {
    
    
					resultSet.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(statement != null) {
    
    
				try {
    
    
					statement.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
    
    
				try {
    
    
					connection.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

package com.sxuek.study;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

/**
 
 * @author 11018
 *
 */
public class Demo03 {
    
    
	public static void main(String[] args) {
    
    
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
    
    
			String username = JOptionPane.showInputDialog("请输入用户名");
			String password = JOptionPane.showInputDialog("请输入密码");
			
			//1、加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2、创建和数据库之间的连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:33006/school?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8","root","123456");
			//3、准备查询语句
			String sql = "select * from user where user_name=? and password=?";
			//4、准备小推车-顺带带着SQL语句一起准备了 这样的话会把SQL语句给提前编译了
			prepareStatement = connection.prepareStatement(sql);
			//需要替换占位符
			prepareStatement.setString(1, username);
			prepareStatement.setString(2, password);
			
			//5、执行SQL语句得到返回结果
			resultSet = prepareStatement.executeQuery();
			/**
			 * 6、处理返回结果--获取结果集中所有虚拟表格的数据
			 */
			while(resultSet.next()) {
    
    
				//获取当前行的数据
				String user = resultSet.getString("user_name");
				String pass = resultSet.getString("password");
				System.out.println(user+pass);
			}
			
		} catch (ClassNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			//7、关闭资源
			if(resultSet != null) {
    
    
				try {
    
    
					resultSet.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(prepareStatement != null) {
    
    
				try {
    
    
					prepareStatement.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
    
    
				try {
    
    
					connection.close();
				} catch (SQLException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/cai_4/article/details/131445786
今日推荐