JAVA TestOperation代码 P147内容

JAVA TestOperation代码  P147内容

驱动加载成功
数据库连接成功
插入一条记录成功~!
1002
1003
1004
1005
断开数据库~!

package week5.day3;

import java.sql.*;

public class TestOperation {
    Connection con;
    Statement stmt;
    ResultSet rs;

    private final String m_JDBCDriver = "com.mysql.cj.jdbc.Driver";
    private final String url = "jdbc:mysql://mysql.sqlpub.com:3306/a000022";
    private final String user = "a000022";
    private final String password = "e49cad4cc824f2d0";

    public TestOperation() {
        //完成驱动

        try {
            Class.forName(m_JDBCDriver);
            System.out.println("驱动加载成功");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 连接库,如果连接成功,则返回真,不然假。
     *
     * @return 布尔开型
     */
    public boolean connect() {

        if (con != null)
            return true;

        try {
            con = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功");
            stmt = con.createStatement();

        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            return false;
        }
        return true;
    }


    /**
     * 断开连接
     */

    public void disconnect() {

        if(rs!=null){
            try {
                rs.close();
                rs=null;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            stmt=null;
        }


        if (con != null) {
            try {
                con.close();
                System.out.println("断开数据库~!");
            } catch (SQLException e) {
                System.out.println("断开失败~!");
                throw new RuntimeException(e);
            }
            con = null;
        }
    }

    public ResultSet getResult(String sql) {
        try {
            rs = stmt.executeQuery(sql);
            return rs;
        } catch (SQLException e) {
            System.out.println("sql语句附近可能有语法错误。");
            return null;
        }
    }

    /**
     * 更新记录
     *
     * @param sql
     * @return
     */
    public boolean updateSql(String sql) {
        try {
            stmt.executeUpdate(sql);
            //con.commit();//立即相应
            return true;
        } catch (SQLException e) {
            System.out.println("sql语句[updateSql]附近可能有语法错误。");
            return false;

        }

    }

    public boolean inserSql(String sql) {
        try {
            stmt.executeUpdate(sql);
            //con.commit();//立即相应
            return true;
        } catch (SQLException e) {
            System.out.println("sql语句[inserSql]附近可能有语法错误。");
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
        TestOperation test = new TestOperation();

        String sql1 = "insert into userTable(userID, userName, userPassword) value(1004,'admin2','admin2')";
        String sql2;

        ResultSet rs;
        if (!test.connect())
            System.exit(1);


        if (!test.inserSql(sql1))//执行插入
        {

            test.disconnect();
            System.exit(1);
        }
        System.out.println("插入一条记录成功~!");


        sql2 = "select * from userTable";
        rs = test.getResult(sql2);
        if (rs != null) {//如果有记录
            try {
                while (rs.next()) {
                    System.out.println(rs.getInt(1));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        test.disconnect();//关闭一切资源

    }

}

你可能感兴趣的:(java)