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;
|
import com.example.springboot.entity.north_glass_buffer1;
|
@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 north_glass_buffer1 selectGlass(int glassid) throws SQLException {
|
conn = getConn();
|
north_glass_buffer1 glass=new north_glass_buffer1();
|
String sql = "select ordernumber from north_glass_buffer1 where glassid=?";
|
ps = conn.prepareStatement(sql);
|
ps.setInt(1, glassid);
|
rs= ps.executeQuery();
|
while (rs.next()) {
|
glass.setordernumber(rs.getString("ordernumber"));
|
}
|
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();
|
}
|
}
|
|
|
}
|