java学习之jdbc
0x00前言
在一些web开发或者是数据存储的时候,肯定会使用到数据库来进行数据存储。而在Java里面需要调用JDBC来对数据库进行操作。每次用jdbc很麻烦,就可以采用一些连接池来解决这个问题
0x01常用类和方法
0x1Connection接口
1.createStatement()
创建一个 Statement对象,用于将SQL语句发送到数据库。
2.close()
Connection发布此 Connection对象的数据库和JDBC资源,而不是等待它们自动释放。
3.CallableStatement prepareCall(String sql)
创建一个调用数据库存储过程的 CallableStatement对象。
4.prepareStatement(String sql)
创建一个 PreparedStatement对象,用于将参数化的SQL语句发送到数据库。 对sql语句进行预处理,解决sql注入问题
5.boolen execute(String sql)
执行的select语句就返回true,其他返回false
6.ResultSet executeQuery(String sql)
执行给定的(select)SQL语句,该语句返回单个 ResultSet对象,
7.int executeUpdate(String sql)
执行给定的SQL语句,这可能是 INSERT , UPDATE ,或 DELETE语句,或者不返回任何内容,如SQL DDL语句的SQL语句。返回的int是影响的函数
8.批处理的执行
(1)void addBatch(String sql)
将给定的SQL命令添加到此 Statement对象的当前命令列表中。
(2)int[] executeBatch()
将一批命令提交到数据库以执行,并且所有命令都执行成功,返回一个更新计数的数组。
(3)void clearBatch()
清空此 Statement对象的当前SQL命令列表。
0x2ResultSet
1.表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
2.boolean next()是否还有下一行数据存在返回true
3.getString(String key);查找指定的列名的数据
public class JVAV_Test01 {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "zhonglin");
//执行sql语句的对象
Statement statement = connection.createStatement();
String s=("select * from web_1.student;");
ResultSet resultSet = statement.executeQuery(s);
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
System.out.println(resultSet.getString("id"));
}
statement.close();
resultSet.close();
}
}
0x02抽取工具类和用配置文件使用
0x1Properties类
在使用jdbc的时候会存在很多重复利用的代码我们就可以把重复的代码给写成一个类供我们使用,同时使用配置文件去灵活的配置数据库文件。
0x2常用方法
1.String getProperty(String key)
使用此属性列表中指定的键搜索属性
2.void list(PrintStream out)
将此属性列表打印到指定的输出流。
public class Jdbc_toll_class {
private static final String drvierClassname;
private static final String user;
private static final String password;
private static final String url;
static {
Properties properties=new Properties();//专门处理配置文件的一个类
try {
properties.load(new FileInputStream("C:\\Users\\**\\IdeaProjects\\sec_Reptile\\src\\main\\resources\\db.properties"));//读取配置文件路径
} catch (IOException e) {
e.printStackTrace();
}
drvierClassname=properties.getProperty("drvierClassname");
user=properties.getProperty("user");
password=properties.getProperty("password");;
url=properties.getProperty("url");;
}
public static void loadDriver(){
try {
Class.forName(drvierClassname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnecton() {
Connection connection = null;
try {
loadDriver();
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}return connection;
}
public static void release(Statement statement,Connection connection){
try {
connection.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void release(ResultSet resultSet, Statement statement, Connection connection){
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
0x03public interface PreparedStatement
0x1方法基础使用
1.int executeUpdate()
执行在该SQL语句PreparedStatement对象,它必须是一个SQL数据操纵语言(DML)语句,如INSERT , UPDATE或DELETE ; 或不返回任何内容的SQL语句,例如DDL语句。
2.boolean execute()
执行此 PreparedStatement对象中的SQL语句,这可能是任何类型的SQL语句。
3.ResultSet executeQuery()
执行此 PreparedStatement对象中的SQL查询,并返回查询 PreparedStatement的 ResultSet对象。
4.void setInt(String,byte)(int parameterIndex, int x)
将指定的参数设置为给定的Java int(String,byte)值。
public class JAVA_test03 {
public static void main(String[] args) {
Connection connection;
PreparedStatement preparedStatement;
connection=Jdbc_toll_class.getConnection();
String sql="insert into student values (null ,?)";
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,"hellow");
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
0x04批处理
1.首先要在的数据库后面加上:?rewriteBatchedStatements=true
2.PreparedStatement的批处理是利用设置参数和循坏去处理
3.如果批处理命令过多要执行后清除再添加
public class JAVA_tese06 {
//批处理后面要加需要在连接的数据库后面加上:?rewriteBatchedStatements=true
public static void main(String[] args) {
Connection connection = Jdbc_toll_class.getConnection();
PreparedStatement preparedStatement=null;
Jdbc_toll_class.executesql("use tese");
String sql="insert into user values(null,?)";
try {
preparedStatement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
for (int i = 0; i < 10; i++) {
preparedStatement.setString(1,"试试");
preparedStatement.addBatch();
if (i %10==0){
preparedStatement.execute();
preparedStatement.clearBatch();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
Jdbc_toll_class.release(connection,preparedStatement);
}
}
}
4.Statement批处理
5.是用addbatch,方法去把批处理命令都添加进去,用executeBatch执行
public class JAVA_test05 {
public static void main(String[] args) {
Connection connection = Jdbc_toll_class.getConnection();
try {
Statement statement = connection.createStatement();
String sql2="use tese";
String sql4="insert into user values(null,'aaa')";
String sql5="insert into user values(null,'bbb')";
String sql6="insert into user values(null,'ccc')";
String sql7="update user set name='mmm' where id=2";
String sql8="delete from user where id=1";
statement.addBatch(sql2);
statement.addBatch(sql4);
statement.addBatch(sql5);
statement.addBatch(sql6);
statement.addBatch(sql7);
statement.addBatch(sql8);
statement.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
0x05连接池
0x1c3p0连接池
public class c3p0_test {
1.获取对象2.还是通过连接对象connection去获取prepareStatement等对象去执行3.它的配置文件需要放在src下面。
public static void main(String[] args) throws SQLException {
DataSource dataSource=new ComboPooledDataSource();//这里可以选择你的配置文件里面预先设定好的配置
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
0x2druid连接池
1.获取对象是通过Properties类获取文件
2.InputStream resourceAsStream = Druid_test.class.getClassLoader().getResourceAsStream("druid.properties");
3.properties.load(resourceAsStream);//构建配置文件
public class Druid_test {
public static void main(String[] args) {
Properties properties=new Properties();
InputStream resourceAsStream = Druid_test.class.getClassLoader().getResourceAsStream("druid.properties");
try {
properties.load(resourceAsStream);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.工具类的定义
1.封装一些方法方便我们使用方法
public class JDBCUtils {
//1定义成员变量
private static DataSource dataSource;
static {
try {
Properties properties=new Properties();
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
dataSource= DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}public static Connection getconnection() throws SQLException {
return dataSource.getConnection();
}
public static void close(Statement statement,Connection connection){
if (statement!=null){
try {
statement.close();
}catch (Exception E){
E.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection){
if (statement!=null){
try {
statement.close();
}catch (Exception E){
E.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
0x3JDBCtemplate
1.用Spring框架集成的这个JDBCtemplate,只需要获取一个Source对象就可以直接执行sql语句
2。它会自己获取连接对象和释放连接对象,
public class JDBCtemplate {
public static void main(String[] args) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
String sql="insert into user values (null,?)";
int i = jdbcTemplate.update(sql, "小天");
System.out.println(1);
}
0x06总结
总结下来其实Spring框架的JDBCtemplate使用起来最简单,以后在有框架开发的时候会用到这些,开发的时候多使用 preparedStatement,预防,sql注入。
本文作者:不成大哥不改名
本文链接:https://www.cnblogs.com/0x3e-time/p/16201010.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步