MySQL-day04am_使用jdbc连接数据库与操作事务

1、jdbc连接数据库

    public static void main(String[] args) throws ClassNotFoundException,
            SQLException {
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库,创建连接数据库的对象
        /**
         *       Connection root =
         *        DriverManager.getConnection("jdbc:mysql://IP地址:端口号/数据库名称",
         *           mysql_username, mysql_password);
         */
        Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");
        //获取执行SQL语句的对象
        Statement statement = root.createStatement();
        //执行SQL语句
        String sql = " select  * from students";
/*        String sql2 =
                "insert into students values (1,'张三',18)";*/
        ResultSet rs = statement.executeQuery(sql);
        while(rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("stu_name");
            String gender = rs.getString("age");
            System.out.println("id:"+id+" 姓名:"+name+" 年龄:"+gender);
        }
        //释放资源
        rs.close();
        statement.close();
        root.close();

    }

2、jdbc工具类

/** * jdbc连接本机数据库工具类 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;


public class JDBCUtil {
  /* public static void main(String[] args) {

    }*/

    private static String url;//访问地址
    private static String user;//当前用户名
    private static String pwd;//当前数据库密码
    private static String driver;

    static {
        //初始化数据
        url = "jdbc:mysql://localhost:3306/myscholl2";
        user = "root";
        pwd = "root";
        driver = "com.mysql.jdbc.Driver";
        //  System.out.println(url+user+pwd+driver);
    }

    /**
     * 获取Connection
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 增删改
     *
     * @param connection 连接对象
     * @param sql        执行相关操作的mysql语句
     * @param objects 任意类参数
     * @return
     */
    public static int addDeleteUpdate(Connection connection, String sql, Object... objects) throws SQLException {
        int count = 0;
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        int index = 1;
        if (objects != null) {
            for (Object o : objects
            ) {
                preparedStatement.setObject(index, o);
                index++;
            }
        }
        count = preparedStatement.executeUpdate();
        return count;
    }

    /**
     * 查询
     *
     * @param connection
     * @param sql
     * @param objects
     * @return
     * @throws SQLException
     */
    public static ResultSet query(Connection connection, String sql, Object... objects) throws SQLException {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        int index = 1;
        if (objects != null) {
            for (Object o : objects
            ) {
                preparedStatement.setObject(index, o);
                index++;
            }
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    /**
     * 释放空间
     */
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
        if (resultSet != null) {
            resultSet.close();
        }
        if (connection != null)
            connection.close();
    }}

3、使用JDBCUtill操作事务

public class UtileTest {
/*
    private static String url;//访问地址
    private static String user;//当前用户名
    private static String pwd;//当前数据库密码
    private static String driver;

    static {
        //初始化数据
        url = "jdbc:mysql://localhost:3306/myscholl2";
        user = "root";
        pwd = "root";
        driver = "com.mysql.jdbc.Driver";
        //  System.out.println(url+user+pwd+driver);
    }*/
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//获得Connection对象
       // Class.forName(driver);
        Connection updatedatabase =JDBCUtil.getConnection();
        //开启事务
        updatedatabase.setAutoCommit(false);
//执行相关操作
        int i=JDBCUtil.addDeleteUpdate(updatedatabase,"update emp  set sal=? where  empno=?",12000,7698);
       // System.out.println(i);
 if (i>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        updatedatabase.commit();
        //释放空间
        JDBCUtil.close(updatedatabase,null,null);
    }
}

你可能感兴趣的:(笔记,mysql,数据库,java)