python建设购物网站,网站建设与管理案例教程期末考试,自己做网站的优势,山西网站建设设计JDBC和连接池
大纲
JDBC连接数据库的方式JDBCUtils事务
具体案例
JDBC
需求#xff1a;满足Java程序能对多个不同的数据库进行操作#xff0c;而创建了一种接口#xff0c;实现对数据库的规范
连接数据库的方式
1.方法1
先创建一个Driver对象#xff0c;然后设置…JDBC和连接池
大纲
JDBC连接数据库的方式JDBCUtils事务
具体案例
JDBC
需求满足Java程序能对多个不同的数据库进行操作而创建了一种接口实现对数据库的规范
连接数据库的方式
1.方法1
先创建一个Driver对象然后设置连接到的数据库的地址然后创建一个properties对象在里面设定好账户密码然后通过driver的connect方法创建出connect连接
public class jdbc01 {public static void main(String[] args) throws SQLException {// 前置工作在项目下创建文件夹然后将jar文件拷贝到该目录下// 然后将其加入到项目中// 1.注册驱动Driver driver new Driver();// 2.得到连接// (1)jdbc:mysql://表示表示规定好的协议// (2)localhost 应该是ip地址(这里是主机的ip地址)// (3)3306表示MySQL监听的端口// (4)test db 是指连接到MySQL的哪个数据库// (5)本质上是进行socket连接String url jdbc:mysql://localhost:3306/test01;// 将用户名和密码封装到一个Properties对象中Properties properties new Properties();// user和password是规定好的后面的值根据实际情况properties.setProperty(user,root);properties.setProperty(password, );Connection connect driver.connect(url, properties);// 3.执行sql语句String sql insert into actor values(null,刘德华,男,1970-11-11,110);// Statement 用于执行静态sql语句并返回生成的结果的对象Statement statement connect.createStatement();int rows statement.executeUpdate(sql);// 如果是dml语句返回的就是影响到的行数System.out.println(rows 0? 成功:失败);//4.关闭连接statement.close();connect.close();}
}缺点driver是第三方的依赖性强灵活性差
2.使用反射机制 public class jdbc02 {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {Class? aClass Class.forName(com.mysql.jdbc.Driver);Driver driver (Driver) aClass.newInstance();String url jdbc:mysql://localhost:3306/test01;Properties properties new Properties();properties.setProperty(user,root);properties.setProperty(password,);Connection connect driver.connect(url, properties);System.out.println(connect);}
}3.使用DriverManager替换Driver
这种方法具有更好的拓展性
public class jdbc03 {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {Class? aClass Class.forName(com.mysql.jdbc.Driver);Driver driver (Driver) aClass.newInstance();String url jdbc:mysql://localhost:3306/test01;String user root;String password ;// 也可以还是使用properties来存储账户和密码最后在DriverManager的getConnection方法里传入url和propertiesDriverManager.registerDriver(driver);Connection connection DriverManager.getConnection(url, user, password);System.out.println(connection);}
}
4.自动注册简化操作(推荐使用)
在反射时完成了类的加载在静态代码块里实现了自动注册
public class jdbc04 {public static void main(String[] args) throws ClassNotFoundException, SQLException {Class.forName(com.mysql.jdbc.Driver);// 可以不写String url jdbc:mysql://localhost:3306/test01;String user root;String password lei2483034010;Connection connection DriverManager.getConnection(url, user, password);System.out.println(connection);}
}
5.使用配置文件(最推荐)
在4方法的基础上使用配置文件来存储账户和密码更加的灵活
public class jdbc05 {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {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(com.mysql.jdbc.Driver);Connection connection DriverManager.getConnection(url, user, password);System.out.println(connection);}
}
执行sql语句 实际开发中基本不使用statement因为它不能预防sql注入 所以使用preparedStarement来防止sql的注入 使用这个类的好处
public class PreparedStatement {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {Scanner myScanner new Scanner(System.in);System.out.println(请输入账号);String account myScanner.nextLine();System.out.println(请输入密码);String pwd myScanner.nextLine();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(com.mysql.jdbc.Driver);Connection connection DriverManager.getConnection(url, user, password);String sqlSelect select name,pwd from admin where name ? and pwd ?;java.sql.PreparedStatement preparedStatement connection.prepareStatement(sqlSelect);// 赋值preparedStatement.setString(1,account);preparedStatement.setString(2,pwd);ResultSet resultSet preparedStatement.executeQuery();// 得到一个查询到resultSet集if (resultSet.next()){System.out.println(恭喜,登录成功);}else {System.out.println(对不起登录失败);}resultSet.close();preparedStatement.close();connection.close();}
} JDBCUtils
把JDBC的连接数据库操作和关闭资源封装到一个工具类中
public class JDBCUtils {//定义相关的属性(4个), 因为只需要一份因此我们做出staticprivate static String user; //用户名private static String password; //密码private static String url; //urlprivate static String driver; //驱动名//在static代码块去初始化static {try {Properties properties new Properties();properties.load(new FileInputStream(src\\mysql.properties));//读取相关的属性值user properties.getProperty(user);password properties.getProperty(password);url properties.getProperty(url);driver properties.getProperty(driver);} catch (IOException e) {//在实际开发中我们可以这样处理//1. 将编译异常转成 运行异常//2. 调用者可以选择捕获该异常也可以选择默认处理该异常比较方便.throw new RuntimeException(e);}}//连接数据库, 返回Connectionpublic static Connection getConnection() {try {return DriverManager.getConnection(url, user, password);} catch (SQLException e) {//1. 将编译异常转成 运行异常//2. 调用者可以选择捕获该异常也可以选择默认处理该异常比较方便.throw new RuntimeException(e);}}//关闭相关资源/*1. ResultSet 结果集2. Statement 或者 PreparedStatement3. Connection4. 如果需要关闭资源就传入对象否则传入 null*/public static void close(ResultSet set, Statement statement, Connection connection) {//判断是否为nulltry {if (set ! null) {set.close();}if (statement ! null) {statement.close();}if (connection ! null) {connection.close();}} catch (SQLException e) {//将编译异常转成运行异常抛出throw new RuntimeException(e);}}}
事务Java中使用 public class Transaction_ {//没有使用事务.Testpublic void noTransaction() {//操作转账的业务//1. 得到连接Connection connection null;//2. 组织一个sqlString sql update account set balance balance - 100 where id 1;String sql2 update account set balance balance 100 where id 2;PreparedStatement preparedStatement null;//3. 创建PreparedStatement 对象try {connection JDBCUtils.getConnection(); // 在默认情况下connection是默认自动提交preparedStatement connection.prepareStatement(sql);preparedStatement.executeUpdate(); // 执行第1条sqlint i 1 / 0; //抛出异常preparedStatement connection.prepareStatement(sql2);preparedStatement.executeUpdate(); // 执行第3条sql} catch (SQLException e) {e.printStackTrace();} finally {//关闭资源JDBCUtils.close(null, preparedStatement, connection);}}//事务来解决Testpublic void useTransaction() {//操作转账的业务//1. 得到连接Connection connection null;//2. 组织一个sqlString sql update account set balance balance - 100 where id 1;String sql2 update account set balance balance 100 where id 2;PreparedStatement preparedStatement null;//3. 创建PreparedStatement 对象try {connection JDBCUtils.getConnection(); // 在默认情况下connection是默认自动提交//将 connection 设置为不自动提交connection.setAutoCommit(false); //开启了事务preparedStatement connection.prepareStatement(sql);preparedStatement.executeUpdate(); // 执行第1条sqlint i 1 / 0; //抛出异常preparedStatement connection.prepareStatement(sql2);preparedStatement.executeUpdate(); // 执行第3条sql//这里提交事务connection.commit();} catch (SQLException e) {//这里我们可以进行回滚即撤销执行的SQL//默认回滚到事务开始的状态.System.out.println(执行发生了异常撤销执行的sql);try {connection.rollback();} catch (SQLException throwables) {throwables.printStackTrace();}e.printStackTrace();} finally {//关闭资源JDBCUtils.close(null, preparedStatement, connection);}}
}