Java连接,操作MySQL

基本步骤

使用Java操作MySQL按步骤分为:

  • 加载驱动
  • 建立连接
  • 创建Statement对象
  • 传递SQL语句,并获取执行的结果集
  • 遍历结果集
  • 关闭Statement对象
  • 关闭连接

实例

1.导入jar包

mysql-connector-java-8.0.16.jar
链接:https://pan.baidu.com/s/1jVDh3KbRgtNpXnjVudQPjg
提取码:pxba

  • 在项目中新建lib文件夹,并且复制粘贴jdbc包在文件夹中
    在这里插入图片描述
  • 右键单击lib,选择 Add as libary…
    在这里插入图片描述
  • 按 ok就行
    在这里插入图片描述

2.上代码

建表语句 :

CREATE TABLE `websites` (
	`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
	`name` CHAR ( 20 ) NOT NULL DEFAULT '' COMMENT '站点名称',
	`url` VARCHAR ( 255 ) NOT NULL DEFAULT '',
	`alexa` INT ( 11 ) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名(全球排名)',
	`country` CHAR ( 10 ) NOT NULL DEFAULT '' COMMENT '国家',
	PRIMARY KEY ( `id` ) 
) ENGINE = INNODB AUTO_INCREMENT = 10 DEFAULT CHARSET = utf8;

INSERT INTO `websites`
VALUES
	( '1', 'Google', 'https://www.google.com/', '1', 'USA' ),
	( '2', '淘宝', 'https://www.taobao.com/', '9', 'CN' ),
	( '3', '百度', 'https://www.baidu.com/', '6', 'CN' ),
	( '4', '微博', 'http://weibo.com/', '16', 'CN' ),
	( '5', 'Facebook', 'https://www.facebook.com/', '4', 'USA' ),
	( '6', 'alexa', 'http://www.alexa.cn/', '86287', 'CN' );
	
COMMIT;#使用commit语句,提交事务

在这里插入图片描述

java代码 :

package MySQL;

import java.sql.*;

public class a {

	//加载驱动 
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
	// aaa 为数据库名
    static final String DB_URL = "jdbc:mysql://localhost:3306/aaa";


    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "student";
    static final String PASS = "aaaaaaaa";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT * FROM websites";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
                int alexa = rs.getInt("alexa");
                String country = rs.getString("country");
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print(", 站点 排名: " + alexa);
                System.out.print(", 站点 国家: " + country);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}

3.运行结果

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章