中科宁波网站建设,友情链接购买,d8 wordpress,关于重新建设网站的申请表目录
一.JDBC概述
二.JDBC API
三.ResultSet[结果集]
四.Statement 五.PreparedStatement
六. JDBC API 总结 一.JDBC概述
JDBC 为访问不同的数据库提供了同一的接口#xff0c;为使用着屏蔽了细节问题Java程序员使用JDBC 可以连接任何提供了 JDBC驱动的数据库系统为使用着屏蔽了细节问题Java程序员使用JDBC 可以连接任何提供了 JDBC驱动的数据库系统从而完成对数据库的各种操作JDKC的原理图模拟JDBC com.hspedy.jdbc.myjdbc
JDBC是java操作提供的一套用于数据库操作的接口API,java程序员只需要面向这套接口编程计科。不同的数据库厂商需要针对这套接口提供不同实现。
二.JDBC API
JDBC API 是一系列接口她同一和规范了应用程序于数据的连接执行SQL语句并得到返回结果等各种操作相关类和接口在java.sql与 javax.sql 包中 JDBC程序编写步骤
注册驱动 加载Driver类获取连接 得到 connection执行增删改查 发送相关的sql命令给mysql执行释放资源 关闭相关的连接等 package com.tianedu.jdbc;import com.mysql.jdbc.Driver;import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;/*** author tian* 这是第一个JDBC程序 完成简单的操作*/
public class Jdbc01 {public static void main(String[] args) throws SQLException {// 前置工作在项目下创建一个文件夹 libs// 将 mysql.jar 拷贝到该目录下点击 add to project 加入到项目中才可以使用//1.注册驱动Driver driver new Driver(); // 创建driver 对象//2.得到连接//(1) jdbc: // 规定好的协议通过jdbc的方式连接mysql//(2) localhost 主机也可以是ip地址//3 3306 表示MySQL 监听的端口//4 hsp_db03 表示连接到呢个数据库// 5 MySQL的连接本质就是前面学习过sql 的连接String url jdbc:mysql://localhost:3306/hsp_db03;// 将用户名和密码 放到Properties 对象中Properties properties new Properties();// 说明 user 和 password 是规定好的后面的值根据实际情况写properties.setProperty(user,root); // 用户properties.setProperty(password,tian); //密码Connection connect driver.connect(url, properties);//3.执行sql语句//String sql insert into actor values(null,刘德华,男,1970-11-11,110);// String sql update actor set name 周星驰where id 1;String sql delete from actor where id 1;// 下面的statement 用于执行静态的sql 语句并返回其生成的结果的对象Statement statement connect.createStatement();int rows statement.executeUpdate(sql); // 如果是dml语句返回的就是影响行数 如果是1 添加成功 如果是0 添加失败System.out.println(rows 0 ? 成功:失败);//4.关闭资源statement.close();connect.close();}
}获取数据库连接5种方式
package com.tianedu.jdbc;import com.mysql.jdbc.Driver;
import org.junit.Test;import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;/*** author tian* 分析java 连接MySQL的物种方式*/
public class JdbcConn {Testpublic void connect01() throws SQLException {Driver driver new Driver();String url jdbc:mysql://localhost:3306/hsp_db03;// 将用户名和密码放入到Properties 对象Properties properties new Properties();properties.setProperty(user, root);properties.setProperty(password, tian);Connection connect driver.connect(url, properties);System.out.println(connect);}//方式2Testpublic void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {//使用反射加载Driver 动态加载更加灵活减少依赖性Class? aClass Class.forName(com.mysql.jdbc.Driver);Driver driver (Driver)aClass.newInstance();String url jdbc:mysql://localhost:3306/hsp_db03;// 将用户名和密码放入到Properties 对象Properties properties new Properties();properties.setProperty(user, root);properties.setProperty(password, tian);Connection connect driver.connect(url, properties);System.out.println(方式2 connect);}// 方式3 使用DriverManager 替代 Driver 进行统一管理Testpublic void connect03() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {// 使用反射 加载DriverClass? aClass Class.forName(com.mysql.jdbc.Driver);Driver driver (Driver) aClass.newInstance();//创建 url 和 user 和 passwordString url jdbc:mysql://localhost:3306/hsp_db03;String user root;String password tian;DriverManager.registerDriver(driver); //注册Driver 驱动Connection connection DriverManager.getConnection(url, user, password);System.out.println(第三种方式 connection);}// 方式4 使用Class.forName 自动完成注册简化代码// 使用最多Testpublic void connect04() throws ClassNotFoundException, SQLException {// 使用反射加载 Driver 类// 在加载Driver类时完成注册/*源码 1.静态代码块在类加载时会执行一次2.DriverManager.registerDriver(new Driver());3.因此 加载和注册Driver 的工作已经完成static {try {DriverManager.registerDriver(new Driver());} catch (SQLException var1) {throw new RuntimeException(Cant register driver!);}}*/Class.forName(com.mysql.jdbc.Driver) ; //如果没有这句也可以执行建议写上更加明确// MySQL 驱动 5.1.6 可以无需Class.forName(com.mysql.jdbc.Driver);// 从 jdk1.5 以后使用Jdbc4 不在需要显示调用class.forName() 注册驱动而已自动调用驱动// jar包下META-INF\services\java.sql.Driver文本种的类 名称去注册String url jdbc:mysql://localhost:3306/hsp_db03;String user root;String password tian;Connection connection DriverManager.getConnection(url,user,password);System.out.println(第四种方式 connection);}Test// 方式5在方式4的基础上改进增加配置文件让信息连接MySQL更加灵活public void connect05() throws IOException, ClassNotFoundException, SQLException {//通过Properties 对象获取配置文件的信息Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));// 获取相关的值String user properties.getProperty(user);String password properties.getProperty(password);String driver properties.getProperty(driver);String url properties.getProperty(url);Class.forName(driver); //建议写上 更加明确Connection connection DriverManager.getConnection(url, user, password);System.out.println(方式5 connection);}
}三.ResultSet[结果集]
表示数据库结果的数据表通常通过执行查询数据库的语言生成ResultSet对象保持一个光标指向其当中的数据行最初给光标位于第一行之间next方法将光标移动到下一行并且由于在ResultSet对象种没有更多行时返回false因此可以在while 循环中使用循环来遍历结果集
package com.tianedu.jdbc.resultest_;import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;/*** author tian** 演示select 语句返回一个resultset 并取出结果*/
SuppressWarnings({all})
public class ResultSet_ {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {//通过Properties 对象获取配置文件Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));// 获取相关的值String user properties.getProperty(user);String password properties.getProperty(password);String driver properties.getProperty(driver);String url properties.getProperty(url);Class.forName(driver); //建议写上 更加明确Connection connection DriverManager.getConnection(url, user, password);//得到StatementStatement statement connection.createStatement();//组织sql 语句String sql select id,name,sex,borndate from actor;//执行给定的sql语句该语句返回单个 ResultSet对象ResultSet resultSet statement.executeQuery(sql);// 使用where 循环取出数据while (resultSet.next()) {// 让光标向后移动 如果没有更多的记录则返回falseint id resultSet.getInt(1);String name resultSet.getString(2);// 获取该行第二列String sex resultSet.getString(3);Date date resultSet.getDate(4);System.out.println(id \t name \t sex \t date);// 获取该行的对第一列}// 关闭连接resultSet.close();connection.close();statement.close();}
}四.Statement
statement 对象 用于执行静态SQL语句返回生成的结果的对象在连接建立后需要对数据库进行访问执行命令或是sql 语句可以通过 Statement【存在sql注入】PerparedStatement【预处理】CallableStatement【存储过程】Statement对象执行sql语句存在sql 注入风险sql 注入是利用某些系统没有对用户输入的数据进行充分的检查而在用户输入数据中注入非法的sql 语句或命令恶意攻击数据库。sql_injection.sql要防范sql注入只要用PerparedStatement(从Statement 扩展而来取代Statement 就可以了
package com.tianedu.jdbc.statement_;import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;/*** author tian* 演示statement 的注入问题*/
SuppressWarnings({all})
public class Statement_ {public static void main(String[] args) throws Exception {Scanner scanner new Scanner(System.in);//让用户输入管理员姓名和密码System.out.print(请输入管理员的名字);String admin_name scanner.nextLine(); //next当接受到空格或者 表示结束System.out.print(请输入管理员的密码);String admin_pwd scanner.nextLine();// 通过Properties 对象获取配置文件的信息Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));// 获取相关的值String user properties.getProperty(user);String password properties.getProperty(password);String driver properties.getProperty(driver);String url properties.getProperty(url);//1.注册驱动Class.forName(driver);//2.得到连接Connection connection DriverManager.getConnection(url,user,password);//3.得到StatementStatement statement connection.createStatement();//4.组织sqlString sql select name,pwd from admin where name admin_name and pwd admin_pwd ;ResultSet resultSet statement.executeQuery(sql);if(resultSet.next()){//如果查询到一条记录则说明该管理员存在System.out.println(登录成功);} else {System.out.println(对不起登录失败);}// 关闭连接resultSet.close();statement.close();connection.close();}}五.PreparedStatement
PreparedStatement 执行的sql语句中的参数用问号来表示调用PerparedStatement 对象的setXxxx() 方法来设置这些参数。setXxx() 方法有两个参数第一个参数是要设置的sql语句中的索引从1开始第二个是设置的sql 语句中的参数值调用 executeQuery() 返回ResultSet 对象调用 executeUpdate(): 执行更新包括增删修改 预处理好处
不在使用 拼接sql语句减少语法错误有效的解决了sql注入问题大大减少了编译次数效率较高
package com.tianedu.jdbc.preparedstatement_;import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.Scanner;/*** author tian** 演示 preparedStatement使用*/
SuppressWarnings({all})
public class PreparedStatementDML_ {public static void main(String[] args) throws Exception {Scanner scanner new Scanner(System.in);//让用户输入管理员姓名和密码System.out.print(请输入添加的名字);String admin_name scanner.nextLine(); //next当接受到空格或者 表示结束// System.out.print(请输入管理员的密码);//String admin_pwd scanner.nextLine();//通过PropertiesProperties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));//获取相关的值String user properties.getProperty(user);String password properties.getProperty(password);String driver properties.getProperty(driver);String url properties.getProperty(url);// 注册驱动Class.forName(driver);//得到连接Connection connection DriverManager.getConnection(url,user,password);// 组织sql sql语句的 相当于占位符// 添加一个记录//String sql insert into admin values(?,?);//String sql update admin set pwd ? where name ?;String sql delete from admin where name ?;//3.2 preparedStatement 对象实现了 preparedStatement 接口的实现类的对象PreparedStatement preparedStatement connection.prepareStatement(sql);//3.3 给赋值preparedStatement.setString(1,admin_name);//preparedStatement.setString(1,admin_pwd);//4.执行DML 语句使用 executeUpdateint rows preparedStatement.executeUpdate();System.out.println(rows 0 ? 执行成功:执行失败);//关闭连接preparedStatement.close();connection.close();}
}
六. JDBC API 总结
Driver Manager 驱动管理类 getConnection(url,user,pwd) 获取连接Connection 接口 creatStatement 创建Statement 对象使用较少存在注入问题 preparedStatement(sql) 生成一个预处理对象Statement 接口 executeUpdate(sql) 执行dml语句 返回影响得到行数 executeQuery(sql) 执行查询返回ResultSet对象 execute(sql) 执行任意的sql 返回的是布尔值 PreparedStatement 接口 excuteUpdate() 执行Dml语句 excuteQuery() 执行查询 返回ResultSet excute() 执行任意的sql 返回一个布尔值 setXxx(站位符索引占位符的值) 可以解决sql 注入 setObject(站位符的索引,站位符的值) ResultSet(结果集) next() 方法向下移动一行 同时如果没有下一行返回false previous() 向上移动一行 getXX(列的索引 | 列名) 返回对应类的值 接受类型是Xxx getObject(列的索引 | 列名) 返回对应列的值接受类型为object