(JDBC)数据库编程步骤及简单例子

JDBC开发案例:

一.准备数据库环境

drop database if exists `memo`; 
create database if not exists `memo` default character set utf8 collate utf8_general_ci;
 use `memo`;
drop table if exists `memo_group`; 
create table if not exists `memo_group`( id int primary key auto_increment comment '便签组编号', name varchar(8) not null unique key comment '便签组名称', created_time datetime not null comment '创建时间', modify_time timestamp comment '修改时间' )engine innodb;
insert into `memo_group` (id,name,created_time) values (1, '默认组', now());
drop table if exists `memo_info`; 
create table if not exists `memo_info`( id int primary key auto_increment comment '便签编号', group_id int not null comment '便签组编号', title varchar(32) not null comment '便签标题', content varchar(1024) not null default '' comment '便签内容',
is_protected char(1) not null default '0' comment '是否私密,0:公开 1:私密', background enum('WHITE','RED','BLUE','GREEN') default 'WHITE' comment '背景颜 色', is_remind char(1) default '0' comment '是否提醒,0:不提醒 1:提醒', remind_time datetime comment '提醒时间', created_time datetime not null comment '创建时间', modify_time timestamp comment '修改时间' )engine innodb;
insert into `memo_info`(id,group_id,title,content,created_time) values (1,1,'欢迎使 用','下面是使用手册',now()); drop table if exists `memo_share`; 
create table if not exists `memo_share`( id int primary key auto_increment comment '便签分享编号', info_id int comment '便签编号', mark varchar(32) not null default '' comment '分享备注', share_time datetime not null comment '分享时间' )engine innodb;
-- 欢迎标签分享 insert into `memo_share`(id,info_id,mark, share_time) values (1,1,'特别有意思的便签 APP',now());

二 、具体编程实现

分为九步:

//1.加载数据库的JDBC驱动
//2.创建连接
//3.创建命令
//4.准备SQL语句
//5.执行SQL
//6.处理结果
//7.关闭结果
//8。关闭命令
//9.关闭连接
import java.sql.*;
public class MyFirstJdbcCase {
 
    public static void main(String[] args) {
        try {
            //1.
            Class.forName("com.mysql.jdbc.Driver");
            //2.JDBC url 协议
            String url="jdbc:mysql://127.0.0.1:3306/memo?user=root&password=root";
            Connection connection= DriverManager.getConnection ( url );
            //3.创建命令
            Statement statement=connection.createStatement ();
            //4.准备SQL
            String sql="select id ,name,created_time,modify_time from memo_group";
            //5.执行
           ResultSet resultSet= statement.executeQuery ( sql );
            //6.使用
            while(resultSet.next()){
              int id=  resultSet.getInt ( "id" );

          String name=resultSet.getString ( "name" );
         Timestamp createTime=resultSet.getTimestamp ( "created_time" );
         Timestamp modifyTime=resultSet.getTimestamp ( "modify_time" );
                System.out.println (id+""+name+""+createTime+""+modifyTime+"" );

            }
            //7.关闭结果集
            resultSet.close ();
            //8.关闭命令
            statement.close ();
            //9.关闭连接
            connection.close ();
        } catch (ClassNotFoundException e) {
            e.printStackTrace ( );
        } catch (SQLException e) {
            e.printStackTrace ( );
        }
    }

}
发布了129 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/beststudent_/article/details/95812658