jdbc结束

This commit is contained in:
yinkanglong
2023-11-26 18:29:38 +08:00
parent f31bf5475d
commit 8090262bff
61 changed files with 3576 additions and 613 deletions

View File

@@ -50,6 +50,19 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,7 +1,7 @@
package org.example.controller;
import org.apache.commons.lang3.StringUtils;
import org.example.bean.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

View File

@@ -1,10 +1,7 @@
package org.example.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.example.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

View File

@@ -0,0 +1,63 @@
package org.example.dao;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BaseDAO {
public int executeUpdate(String sql, Object... args) throws SQLException {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setString(i, args[i-1].toString());
}
int rows = ps.executeUpdate();
ps.close();
if (!conn.getAutoCommit()) {
conn.close();
}
return rows;
}
public <T> List<T> executeQuery(Class<T> clazz,String sql, Object... args) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setString(i, args[i-1].toString());
}
ResultSet resultSet = ps.executeQuery();
//对返回值进行解析
ResultSetMetaData resultMeta = resultSet.getMetaData();
int count = resultMeta.getColumnCount();
List<T> rows = new ArrayList<T>();
while(resultSet.next()){
T t = clazz.newInstance();
for (int i = 1; i < count; i++) {
Object object = resultSet.getObject(i);
String columnName = resultMeta.getColumnName(i);
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t,object);
}
rows.add(t);
}
ps.close();
if (!conn.getAutoCommit()) {
conn.close();
}
return rows;
}
}

View File

@@ -0,0 +1,45 @@
package org.example.dao;
import com.mysql.cj.jdbc.Driver;
import org.junit.Test;
import java.sql.*;
public class JDBCTest {
@Test
public void testJDBCConnnection() throws SQLException {
DriverManager.registerDriver(new Driver());
String url = "jdbc:mysql://127.0.0.1/test";
String username= "root";
String password = "long1011";
Connection connection = DriverManager.getConnection(url, username, password);
String sql2 = "INSERT INTO user (name) values ('testname02')";
connection.createStatement().executeUpdate(sql2);
String sql = "select * from user;";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String name = resultSet.getString("name");
int id = resultSet.getInt("id");
System.out.println("name: " + name+" id: " + id);
}
resultSet.close();
statement.close();
connection.close();
}
}

View File

@@ -0,0 +1,56 @@
package org.example.dao;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtils {
static DataSource datasource = null;
private static ThreadLocal<Connection> tl = new ThreadLocal<>();
static {
Properties properties = new Properties();
InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try{
properties.load(in);
}catch(IOException e){
throw new RuntimeException(e);
}
try {
datasource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
Connection connection = tl.get();
if(connection == null){
connection = datasource.getConnection();
tl.set(connection);
}
return connection;
}
public static void freeConnection() throws SQLException {
Connection connection = tl.get();
if(connection != null){
tl.remove();
connection.setAutoCommit(false);
connection.close();
}
}
}