mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-29 08:05:04 +08:00
use try-with-resource style for sql queries
This commit is contained in:
@@ -2,8 +2,6 @@ package love.sola.netsupport.sql;
|
|||||||
|
|
||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ***********************************************
|
* ***********************************************
|
||||||
@@ -25,8 +23,4 @@ public class SQLCore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void free(Connection conn) {
|
|
||||||
if (conn != null) try { conn.close(); } catch (SQLException e) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,7 @@ public class TableUser extends SQLCore {
|
|||||||
public static final String COLUMN_WECHAT = "wechat";
|
public static final String COLUMN_WECHAT = "wechat";
|
||||||
|
|
||||||
public static User getUserByName(String name) {
|
public static User getUserByName(String name) {
|
||||||
Connection conn = null;
|
try (Connection conn = ds.getConnection()) {
|
||||||
try {
|
|
||||||
conn = ds.getConnection();
|
|
||||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE name=?");
|
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE name=?");
|
||||||
ps.setString(1, name);
|
ps.setString(1, name);
|
||||||
ResultSet rs = ps.executeQuery();
|
ResultSet rs = ps.executeQuery();
|
||||||
@@ -38,15 +36,12 @@ public class TableUser extends SQLCore {
|
|||||||
ISPType.fromId(rs.getInt(COLUMN_ISP)),
|
ISPType.fromId(rs.getInt(COLUMN_ISP)),
|
||||||
rs.getString(COLUMN_WECHAT));
|
rs.getString(COLUMN_WECHAT));
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) { }
|
||||||
} finally { free(conn); }
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User getUserById(int id) {
|
public static User getUserById(int id) {
|
||||||
Connection conn = null;
|
try (Connection conn = ds.getConnection()) {
|
||||||
try {
|
|
||||||
conn = ds.getConnection();
|
|
||||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE id=?");
|
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE id=?");
|
||||||
ps.setInt(1, id);
|
ps.setInt(1, id);
|
||||||
ResultSet rs = ps.executeQuery();
|
ResultSet rs = ps.executeQuery();
|
||||||
@@ -58,8 +53,7 @@ public class TableUser extends SQLCore {
|
|||||||
ISPType.fromId(rs.getInt(COLUMN_ISP)),
|
ISPType.fromId(rs.getInt(COLUMN_ISP)),
|
||||||
rs.getString(COLUMN_WECHAT));
|
rs.getString(COLUMN_WECHAT));
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) { }
|
||||||
} finally { free(conn); }
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user