clll
2023-11-27 1ebc598658f85c07c7cbabaf289ab89838a8acc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.springboot.service;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import org.springframework.stereotype.Component;
 
import com.example.springboot.entity.Glass;
@Component
public class JdbcConnections {
    /**
     * 数据库连接参数
     * driver,url,username,password
     */
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/canadames";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "beibo.123/";
    
    private static Connection conn = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;
    
    public  Glass selectGlass(int glassid) throws SQLException {
        conn = getConn();
        Glass glass=new Glass();
        String sql = "select orderid from glass where glassid=?";
         ps = conn.prepareStatement(sql);
         ps.setInt(1, glassid);
         rs= ps.executeQuery();
         while (rs.next()) {
            glass.setOrderId(rs.getString("orderid"));
         }
         conn.close();
         return glass;
    }
    /**
     * 1. 加载驱动
     * 2. 获取连接    conn
     * 3. 创建语句 ps
     * 4. 执行语句 rs
     * 5. 处理结果
     * 6. 回收资源
     * 
     * 实现CRUD
     *     更新:
     *         1增加
     *         2删除
     *         3修改
     *  查询:
     *      1. 查一个,一个对象
     *      2. 查一组,做成一个对象列表,查全部
     */
    public static Connection getConn() throws SQLException {
        Connection conn = null;
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/canadames?serverTimezone=GMT%2B8&characterEncoding=utf-8", "root", "beibo.123/");
        return conn;
    }
 
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
 
 
}