新网站怎么让百度收录,微信小程序怎么制作网页,淘宝页面设计模板,wordpress后台访问卡1、第一个JDBC程序
步骤总结#xff1a;
创建测试数据库加载驱动#xff0c;固定写法连接数据库获得执行SQL的对象statement执行SQL对象去执行SQLresultset获得返回结果集释放连接
import java.sql.*;public class Jdbc {public static void main(String args[]) throws C…1、第一个JDBC程序
步骤总结
创建测试数据库加载驱动固定写法连接数据库获得执行SQL的对象statement执行SQL对象去执行SQLresultset获得返回结果集释放连接
import java.sql.*;public class Jdbc {public static void main(String args[]) throws ClassNotFoundException, SQLException {//1、加载数据库驱动固定写法Class.forName(com.mysql.jdbc.Driver);//2、用户信息和url连接数据库使用//注意mysql8.0或以上必须在url加上serverTimezoneUTC否则会出现时区异常因为8.0加了新特性安全性也增加了/*useUnicodetrue characterEncodingutf8serverTimezoneUTCuseSSLtrue*/String urljdbc:mysql://localhost:3306/school?useUnicodetruecharacterEncodingutf8serverTimezoneUTCuseSSLtrue;String usernameroot;String password123456;//3、连接数据库数据库对象connection代表数据库Connection connection DriverManager.getConnection(url,username,password);//4、执行SQL对象 StatementStatement statementconnection.createStatement();System.out.println(数据库连接成功);//5、执行SQL对象去执行SQL可能存在结果查看返回值String sqlSELECT * FROM student;ResultSet resultSetstatement.executeQuery(sql);//ResultSet是一个结果集通常是一个列表while (resultSet.next()){System.out.println(id:resultSet.getObject(id));System.out.println(name:resultSet.getObject(name));System.out.println(age:resultSet.getObject(age));System.out.println();}//6、释放连接资源connection.close();statement.close();resultSet.close();}
}
2、JDBC对象详解
DriverManager //DriverManager.registerDriver(new com.mysql.jdbc.Driver());//1、加载数据库驱动固定写法Class.forName(com.mysql.jdbc.Driver);
//connection 代表数据库
//数据库设置自动提交
//事务提交
//事务回滚
connection.rollback();
connection.commit();
connection.setAutoCommit();URL
String urljdbc:mysql://localhost:3306/school?useUnicodetruecharacterEncodingutf8serverTimezoneUTCuseSSLtrue;
//mysql-3306
//协议主机地址端口号/数据库名参数1参数2参数3
//oracle--1521
//jdbc:oracle:thin:localhost:1521:sidStatement执行SQL对象
String sqlSELECT * FROM student;String findSql;String insertSql;String updateSql;String delSql;ResultSet resultSetstatement.executeQuery(sql);statement.executeQuery(findSql);//执行查询statement.executeUpdate(insertSql);//更新、插入、删除都是用这个statement.execute(delSql);//执行任何SQL//ResultSet是一个结果集通常是一个列表ResultSet查询的结果集封装了我们的所有查询 结果 获得指定的数据类型 resultSet.getObject();//不知道列类型的情况下使用//如果知道列的类型就使用指定的类型resultSet.getString();resultSet.getInt();resultSet.getFloat();resultSet.getDate();resultSet.getObject();遍历指针
resultSet.beforeFirst();//移动到最前面resultSet.afterLast();//移动到最后面resultSet.next();//移动到下一个数据resultSet.previous();//移动到前一行resultSet.absolute(row);//移动到指定行释放资源
//6、释放连接资源connection.close();statement.close();resultSet.close();3、statement对象
jdbc中的statement对象用于向数据库发送SQL语句想完成对数据库的增删改查只需要通过这个对象想数据库发送增删改查语句即可。
Statement对象的executeUpdate方法用于向数据库发送增、删、改的sql语句executeUpdate执行完后将会返回一个整数即增删改语句导致了数据库几行数据发生了变化
Statement.executeQuery方法用于向数据库发送查询语句executeQuery方法返回代表查询结构的ResultSet对象
工具类
package com.test.util;import java.io.InputStream;
import java.sql.*;
import java.util.Properties;public class JdbcUtils {private static String drivernull;private static String urlnull;private static String usernamenull;private static String passwordnull;static {try {//获取到配置文件流InputStream inputStreamJdbcUtils.class.getClassLoader().getResourceAsStream(db.properties);//用关键字new创建properties对象Properties properties new Properties();//load方法将流加载进properties对象properties.load(inputStream);//调用方法获取相关属性driverproperties.getProperty(driver);urlproperties.getProperty(url);usernameproperties.getProperty(username);passwordproperties.getProperty(password);Class.forName(driver);} catch (Exception e) {e.printStackTrace();}}public static Connection getConnection() throws SQLException {return DriverManager.getConnection(url,username,password);}public static void release(Connection connection, Statement statement, ResultSet resultSet){if (resultSet!null){try {resultSet.close();} catch (SQLException e) {throw new RuntimeException(e);}}if (statement!null){try {statement.close();} catch (SQLException e) {throw new RuntimeException(e);}}if (connection!null){try {connection.close();} catch (SQLException e) {throw new RuntimeException(e);}}}
}
CRUD操作-insert
package com.test.util;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import static com.test.util.JdbcUtils.*;public class JdbcInsert {public void insert() {Connection connectionnull;Statement statementnull;ResultSet resultSetnull;try {//1、获取连接connection JdbcUtils.getConnection();} catch (SQLException e) {throw new RuntimeException(e);}//2、获得执行SQL对象try {statement connection.createStatement();String sqlInsertINSERT INTO student(id,name,age) VALUES (6,李白,99);int istatement.executeUpdate(sqlInsert);if (i0){System.out.println(插入成功);}} catch (SQLException e) {throw new RuntimeException(e);}//3、关闭资源release(connection,statement,resultSet);}}
CRUD操作-update
package com.test.util;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static com.test.util.JdbcUtils.release;
public class JdbcUpdate {public void update() {Connection connection null;Statement statement null;ResultSet resultSet null;try {connection JdbcUtils.getConnection();} catch (SQLException e) {throw new RuntimeException(e);}try {statementconnection.createStatement();String sqlUpdateUPDATE student SET name杜甫 WHERE id1;int istatement.executeUpdate(sqlUpdate);if (i0){System.out.println(更新成功);}} catch (SQLException e) {throw new RuntimeException(e);}release(connection,statement,resultSet);}}CRUD操作-delete
package com.test.util;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import static com.test.util.JdbcUtils.release;public class JdbcDel {public void del(){Connection connectionnull;Statement statementnull;ResultSet resultSetnull;//1、调用工具类的方法获取连接try {connectionJdbcUtils.getConnection();} catch (SQLException e) {throw new RuntimeException(e);}try {statementconnection.createStatement();String sqlDelDELETE FROM student WHERE id3;int istatement.executeUpdate(sqlDel);if (i0){System.out.println(删除成功);}} catch (SQLException e) {throw new RuntimeException(e);}release(connection,statement,resultSet);}}
CRUD操作-selecet
package com.test.util;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class JdbcSelect {public void select(){Connection connectionnull;Statement statementnull;ResultSet resultSetnull;try {connectionJdbcUtils.getConnection();} catch (SQLException e) {throw new RuntimeException(e);}try {statementconnection.createStatement();} catch (SQLException e) {throw new RuntimeException(e);}String sqlSelectSELECT * FROM student;try {resultSetstatement.executeQuery(sqlSelect);while (resultSet.next()){System.out.println(id:resultSet.getObject(id)\t);System.out.println(name:resultSet.getObject(name)\t);System.out.println(age:resultSet.getObject(age)\t);System.out.println();}} catch (SQLException e) {throw new RuntimeException(e);}}
}
4、SQL注入问题
sql存在漏洞会被攻击冬至数据泄露SQL会被拼接 or
5、PreparedStatement对象
PreparedStatement可以防止SQL注入效率更高。
package com.test.util;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static com.test.util.JdbcUtils.release;
public class JdbcPsDel {public void psDel() {Connection connection null;PreparedStatement preparedStatement null;ResultSet resultSetnull;try {connectionJdbcUtils.getConnection();//获得连接String sqlDelDELETE FROM student WHERE id?;//定义预编译SQL参数用代替preparedStatementconnection.prepareStatement(sqlDel);//预编译preparedStatement.setInt(1,3);//手动给参数值int i preparedStatement.executeUpdate();//执行if (i0){System.out.println(删除成功);}} catch (SQLException e) {throw new RuntimeException(e);}release(connection,preparedStatement,resultSet);//释放资源}}
6、JDBC操作事务
要么成功要么失败
ACID原则 原子性要么全部完成要么都不完成 一致性总数不变 隔离性多个进程互不干扰 持久性一旦提交不可逆持久化到数据库
隔离性问题 脏读一个事务读取了另一个没有提交的事务 不可重复读在同一个事务内重复读取表中的数据表数据发生了改变 虚读幻读在一个事务内读取到了别人插入的数据导致前后读出来结果不一致
7、数据库连接池