文章目录
JDBC
1、JDBC是什么?
Java DataBase Connectivity(Java语言连接数据库)
2、JDBC的本质是什么?
JDBC是SUN公司制定的一套接口(interface)
java.sql.*; (这个软件包下有很多接口。)
接口都有调用者和实现者。
面向接口调用、面向接口写实现类,这都属于面向接口编程。
为什么要面向接口编程?
解耦合:降低程序的耦合度,提高程序的扩展力。
多态机制就是非常典型的:面向抽象编程。(不要面向具体编程)
建议:
Animal a = new Cat();
Animal a = new Dog();
// 喂养的方法
public void feed(Animal a){ // 面向父类型编程。
}
不建议:
Dog d = new Dog();
Cat c = new Cat();
思考:为什么SUN制定一套JDBC接口呢?
因为每一个数据库的底层实现原理都不一样。
Oracle数据库有自己的原理。
MySQL数据库也有自己的原理。
MS SqlServer数据库也有自己的原理。
....
每一个数据库产品都有自己独特的实现原理。
JDBC的本质到底是什么?
一套接口。
3、JDBC开发前的准备工作,先从官网下载对应的驱动jar包,然后将其配置到环境变量classpath当中。
classpath=.;D:\course\06-JDBC\resources\MySql Connector Java 5.1.23\mysql-connector-java-5.1.23-bin.jar
以上的配置是针对于文本编辑器的方式开发,使用IDEA工具的时候,不需要配置以上的环境变量。
IDEA有自己的配置方式。
4、JDBC编程六步(需要背会)
第一步:注册驱动(作用:告诉Java程序,即将要连接的是哪个品牌的数据库)
第二步:获取连接(表示JVM的进程和数据库进程之间的通道打开了,这属于进程之间的通信,重量级的,使用完之后一定要关闭通道。)
第三步:获取数据库操作对象(专门执行sql语句的对象)
第四步:执行SQL语句(DQL DML....)
第五步:处理查询结果集(只有当第四步执行的是select语句的时候,才有这第五步处理查询结果集。)
第六步:释放资源(使用完资源之后一定要关闭资源。Java和数据库属于进程间的通信,开启之后一定要关闭。)
代码块
JDBC代码-注册驱动获取链接
package JDBC;
import java.sql.*;
public class jbac1 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
try {
//1.注册驱动
Driver driver = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver((driver));
//2.获取链接
/*
url 统一资源 定位符
jdbc:mysql://localhost:3306/xq
jdbc:mysql:// 协议
localhost IP地址 也可以是127.0.0.1
3306 mysql数据库端口号
xq 具体的数据库实例名
* */
String url="jdbc:mysql://localhost:3306/xq";
String user = "root";
String password = "120723";
conn = DriverManager.getConnection(url,user,password);
System.out.println("数据库链接对象 = " + conn);
//3.获取数据库操作对象
stmt = conn.createStatement();
//4 执行sql
String sql = "insert into dept(deptno,dname,loc) values(50,'人事部','北京')";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "成功" : "失败");
//5 处理查询结果集
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//6 释放资源
// 为了保证资源一定释放 在finally语句中关闭资源
//分别对其try...catch
try {
if(stmt != null){
stmt.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if(conn != null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
JDBC代码-执行sql语句
package JDBC;
import java.sql.*;
public class JDBC2 {
public static void main(String[] args) {
//1.注册驱动 获取链接 获取数据库操作对象 执行sql语句
Connection conn = null;
Statement stmt = null;
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
String url = "jdbc:mysql://localhost:3306/xq";
String user = "root";
String password = "120723";
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "delete from dept where deptno = 50";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1?"OK" : "NO");
}catch (SQLException e){
e.printStackTrace();
}finally {
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
JDBC代码-注册驱动的另一种方法
package JDBC;
import java.sql.*;
//注册驱动另一种方式
public class JDBC3 {
public static void main(String[] args) {
//1.注册驱动 获取链接 获取数据库操作对象 执行sql语句
Connection conn = null;
Statement stmt = null;
try{
//注册驱动
// DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//另一种方式注册驱动
//因为参数是字符串,字符串可以写到xx.properties文件中
// 不需要接受返回值,因为我们只想用它的类加载
Class.forName("com.mysql.cj.jdbc.Driver");
//获取链接
String url = "jdbc:mysql://localhost:3306/xq";
String user = "root";
String password = "120723";
conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
}catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
JDBC代码-将数据库信息配置到配置文件中
package JDBC;
import java.sql.*;
import java.util.ResourceBundle;
//将连接数据库的信息配置到配置文件
public class JDBC4 {
public static void main(String[] args) {
//1.注册驱动 获取链接 获取数据库操作对象 执行sql语句
Connection conn = null;
Statement stmt = null;
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
JDBC代码-处理结果集,遍历结果集
package JDBC;
//处理结果集 遍历结果集
import java.sql.*;
import java.util.ResourceBundle;
public class JDBC5 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;//查询集
ResourceBundle bun = ResourceBundle.getBundle("jdbc");
String dri = bun.getString("driver");
String url = bun.getString("url");
String user = bun.getString("user");
String password = bun.getString("password");
try{
Class.forName(dri);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
/*
返回值int executeUpdata(insert/delete/update) 返回数量
返回值 ResultSet executeQuery(select)
*/
String sql = "select deptno,dname,loc from dept";
rs =stmt.executeQuery(sql);//执行查询
while(rs.next()){
// System.out.println(rs.getString(3));
// String s1 = rs.getString(1);
// String s2 = rs.getString(2);
// String s3 = rs.getString(3);
// String s1 = rs.getString("deptno");
// String s2 = rs.getString("dname");
// String s3 = rs.getString("loc");
// System.out.println(s1 + " " + s2 + " " + s3);
int s1 = rs.getInt("deptno");//返回指定类型,和sql表中对应
String s2 = rs.getString("dname");
String s3 = rs.getString("loc");
System.out.println((s1 + 1000) + " " + s2 + " " + s3);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(rs != null){
try{
rs.close();
} catch(Exception e){
e.printStackTrace();
}
}if(stmt != null){
try{
stmt.close();
} catch(Exception e){
e.printStackTrace();
}
}if(conn != null){
try{
conn.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}
}
JDBC代码-用户登录业务介绍
package JDBC;
import com.mysql.cj.util.DnsSrv;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JDBC6 {
public static void main(String[] args) {
Map<String,String > userLoginInfo = initUI();
boolean log = denglu(userLoginInfo);
System.out.println(log ? "成功" : "失败");
}
/**
* 用户登录
* @param userLoginInfo 登录信息
* @return 返回值
*/
private static boolean denglu(Map<String, String> userLoginInfo) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String loginName = userLoginInfo.get("loginName");
String loginPwd = userLoginInfo.get("loginPwd");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xq","root","120723");
stmt = conn.createStatement();
String sql = "select * from t_user where loginName = '"+loginName+"' and loginPwd = '"+ loginPwd +"'";
//System.out.println(sql);
rs = stmt.executeQuery(sql);
return rs.next();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(rs != null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
return false;
}
private static Map<String, String> initUI() {
Scanner s = new Scanner(System.in);
System.out.print("用户名:");
String loginName = s.nextLine();
System.out.print("密码:");
String loginPwd = s.nextLine();
Map<String ,String> userLoginInfo = new HashMap<>();
userLoginInfo.put("loginName",loginName);
userLoginInfo.put("loginPwd",loginPwd);
return userLoginInfo;
}
}
JDBC代码-用户登录结束(安全版)
package JDBC;
import com.mysql.cj.util.DnsSrv;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JDBC66 {
public static void main(String[] args) {
Map<String,String > userLoginInfo = initUI();
boolean log = denglu(userLoginInfo);
System.out.println(log ? "成功" : "失败");
}
/**
* 用户登录
* @param userLoginInfo 登录信息
* @return 返回值
*/
private static boolean denglu(Map<String, String> userLoginInfo) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String loginName = userLoginInfo.get("loginName");
String loginPwd = userLoginInfo.get("loginPwd");
boolean f =false ;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xq","root","120723");
// 一个问好代表一个占位符
String sql = "select * from t_user where loginName = ? and loginPwd = ? ";
ps = conn.prepareStatement(sql);
// 给占位符传值 第一个?下表是1 第二个?下表是2....
ps.setString(1,loginName);
ps.setString(2,loginPwd);
//执行sql
rs = ps.executeQuery();
return rs.next();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(rs != null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(ps != null){
try{
ps.close();
}catch(SQLException e){
e.printStackTrace();
}
// 123 or '1'='1'
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
return false;
}
private static Map<String, String> initUI() {
Scanner s = new Scanner(System.in);
System.out.print("用户名:");
String loginName = s.nextLine();
System.out.print("密码:");
String loginPwd = s.nextLine();
Map<String ,String> userLoginInfo = new HashMap<>();
userLoginInfo.put("loginName",loginName);
userLoginInfo.put("loginPwd",loginPwd);
return userLoginInfo;
}
}
MySQL驱动下载
提取码:eara
JAVA JDK-API1.6 版下载
提取码:aggm
有任何问题请添加QQ:81415314