JDBC回顾 2018-08-15

JDBC六步骤:

1、注册数据库驱动
2、连接数据库对象
3、获得执行sql语句
4、执行SQL语句
5、处理执行后的结果
6、 释放资源

public class JDBCTest {

    public static void main(String[] args){
        Connection  conn = null;
        PreparedStatement ps = null;
        try {
            //1:j加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2:连接数据库
            conn = getCollection();
            //3、获得执行sql语句
            String sql = "SELECT * FROM changjiang_account";
            //4、执行SQL语句
            ps = conn.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();
            //5、处理执行后的结果
            String result = doResultSet(resultSet);
            System.out.println(result);
        }catch (Exception e){
            if (ps != null){
                try {
                    ps.close();
                }catch (Exception e1){

                }

            }
            if (conn != null){
                try {
                    conn.close();
                }catch (Exception e2){

                }
            }
        }

    }
    public static Connection getCollection(){
        try {
            String url = "jdbc:mysql://xxx.xxx.xxx:3306/xxx?characterEncoding=utf8";
            String name = "xxxx";//将要连接数据库的账户
            String password = "xxxxxx";//将要连接数据库的密码
            return DriverManager.getConnection(url, name, password);
        }catch (SQLException e){
            System.out.println("获取Connection失败");
        }
        return null;

    }
    public static String doResultSet(ResultSet resultSet) throws SQLException {
        if (resultSet == null){
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        while (resultSet.next()){
            long id = resultSet.getLong("id");
            stringBuffer.append(id + " ");
        }
        return stringBuffer.toString();

    }
}

你可能感兴趣的:(JDBC回顾 2018-08-15)