一个简单的jdbc编程案例

开发工具:idea

数据库:Mysql

1.创建t_role表

DROP TABLE IF EXISTS `t_role`;
CREATE TABLE `t_role` (
  `id` bigint(40) NOT NULL COMMENT '主键',
  `role_name` varchar(40) CHARACTER SET utf8 DEFAULT NULL COMMENT '角色名',
  `note` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2.案例

pom中的依赖:

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>

 

Role对象:

public class Role {

    private Long id;//主键
    private String roleName;//角色名称
    private String note;//备注

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", roleName='" + roleName + '\'' +
                ", note='" + note + '\'' +
                '}';
    }
}

jdbc代码:

 1 package com.lenovo;
 2 
 3 import com.lenovo.pojo.Role;
 4 
 5 import java.sql.*;
 6 
 7 public class JdbcExample {
 8 
 9     public Role getRoleId(Long id){
10         Connection connection = getConnection();
11         PreparedStatement ps = null;
12         ResultSet rs = null;
13         try {
14             ps = connection.prepareStatement("select id,role_name,note from t_role where id = ?");
15             ps.setLong(1,id);
16             rs = ps.executeQuery();
17             while (rs.next()){
18                 long roleId = rs.getLong(1);
19                 String roleName = rs.getString(2);
20                 String note = rs.getString(3);
21                 Role role = new Role();
22                 role.setId(roleId);
23                 role.setRoleName(roleName);
24                 role.setNote(note);
25                 return role;
26             }
27 
28         }catch (Exception e){
29             e.printStackTrace();
30         }finally {
31             close(rs,ps,connection);
32         }
33         return null;
34     }
35 
36     private Connection getConnection(){
37         Connection connection = null;
38         try {
39             Class.forName("com.mysql.jdbc.Driver");
40             String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false";
41             String user = "root";
42             String password = "root";
43             connection = DriverManager.getConnection(url, user, password);
44         }catch (Exception e){
45             System.out.println(JdbcExample.class.getName());
46             e.printStackTrace();
47             return null;
48         }
49         return connection;
50     }
51 
52     private void close(ResultSet rs, Statement st,Connection connection){
53         try {
54             if(rs != null && !rs.isClosed()){
55                 rs.close();
56             }
57         }catch (Exception e){
58             e.printStackTrace();
59         }
60 
61         try {
62             if(st != null && !st.isClosed()){
63                 st.close();
64             }
65         }catch (Exception e){
66             e.printStackTrace();
67         }
68 
69         try {
70             if(connection != null && !connection.isClosed()){
71                 connection.close();
72             }
73         }catch (Exception e){
74             e.printStackTrace();
75         }
76 
77     }
78 
79     public static void main(String[] args) {
80         JdbcExample example = new JdbcExample();
81         Role roleId = example.getRoleId(1l);
82         System.out.println("角色名称:" + roleId.getRoleName());
83     }
84 }

 

执行结果:

 

posted @ 2022-03-09 10:26  永远的希望  阅读(78)  评论(0编辑  收藏  举报