您的位置:首页 > 数据库 > MySQL

JDBC-MySql简单的查询和插入数据

2015-10-18 13:15 711 查看

MySql简单的查询和插入数据

SqlDemo.java

[code]package com.xieth.day01;

import java.sql.*;

public class SqlDemo {
    private Connection conn = null;
    private Statement statement = null;
    private ResultSet set = null;
    private String sql = null;

    public static void main(String[] args) {
        new SqlDemo().QueryRes();
    }

    // 查询结果集操作
    private void QueryRes() {
        statement = null;
        set = null;
        conn = null;
        sql = "select * from person";
        try {
            conn = getConnection();
            statement = conn.createStatement();
            set = statement.executeQuery(sql);
            while (set.next()) {
                System.out.println(set.getString(1) + "->" + set.getString(2));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Realease(conn, statement, set);
        }
    }

    // 插入数据操作
    private void InsertData() {
        conn = null;
        sql = "INSERT INTO Person VALUES ('marry','女');";
        statement = null;
        try {
            conn = getConnection();
            statement = conn.createStatement();
            int Res = statement.executeUpdate(sql);
            System.out.println(Res > 0 ? "插入数据成功" : "插入数据失败");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Realsase(conn, statement);
        }

    }

    // 获取数据库连接
    private Connection getConnection() throws Exception {
        String driverClass = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=GBK";
        String user = "root";
        String password = "123456";

        // 注册加载驱动
        Class.forName(driverClass);

        // 连接数据库
        Connection connection = DriverManager
                .getConnection(url, user, password);

        return connection;
    }

    // 释放资源操作
    private void Realsase(Connection conn, Statement statement) {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    // 重载方法 释放资源
    private void Realease(Connection conn, Statement statement, ResultSet set) {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (set != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


2015年10月18日13:15:00 记录一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: