我创建了一种方法(getData()
)来从数据库接收似乎有效的数据。我想要的是仅使用1个参数,因此该方法应与className.getData("SELECT * FROM myTable")
一起运行。我该怎么办?
public List<String> getData(String sql, Map column) {
this.driver();
try {
Connection conn = DriverManager.getConnection(this.url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<String> result = new ArrayList<>();
if (column.containsKey("string")) {
for (Object i : column.values()) {
while (rs.next()) {
result.add(rs.getString(String.valueOf(i)));
}
}
return result;
}
}
catch (SQLException e) {
e.printStackTrace();
}
return null;
}
private void driver() {
// Ensure we have mariadb Driver in classpath
try {
Class.forName("org.mariadb.jdbc.Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
答案 0 :(得分:5)
确保您不需要将列名作为参数传递。 JDBC允许您从metaData获取列名。就像下面一样
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
proxy_cache_path path/to/cache levels=1:2 keys_zone=mycache:10m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#gzip on;
server {
# charset UTF-8;
listen 8082;
server_name 127.0.0.1;
# location / {
# root html/dist;
# index index.html index.htm;
# try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
# }
location /login {
proxy_set_header Origin 'https://<IPA-HOSTNAME>';
proxy_set_header Referer 'https://<IPA-HOSTNAME>/ipa/ui/';
proxy_set_header Connection 'keep-alive';
proxy_set_header Content-Encoding 'gzip, deflate, br';
proxy_pass https://<IPA-HOSTNAME>/ipa/session/login_password;
proxy_pass_request_headers on;
proxy_pass_request_body on;
proxy_ignore_headers "Set-Cookie" "Cache-Control";
proxy_hide_header "Set-Cookie";
proxy_cache mycache;
proxy_cache_methods GET HEAD POST;
proxy_cache_key "$host$request_uri $request_body";
proxy_cache_valid 200 1d;
proxy_buffering on;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Requested-With,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Requested-With,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range, Date, Server, Connection, IPASESSION';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Requested-With,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range, Date, Server, Connection, IPASESSION';
}
}
location /changePassword {
proxy_set_header Cookie 'ipa_session=MagBearerToken=lqXbNKwd9JjWw2Xdl8uHUk9veiHVvYppkcrXztinDML7Yur1mVcvJDkoYi7LEgOr5Rem3VuP2rHJVuGZVMSY7YHbXoNAFUfK2G3UDt7qaq%2fSAFMPa5%2balJLvi2zS0gDr4jTfQF0WRDhN9RTn8kss1ChIL9v6kjMv%2fDG%2brJ49rFA%3dd';
proxy_pass https://<IPA-HOSTNAME>/ipa/session/change_password;
proxy_set_header Origin 'https://<IPA-HOSTNAME>';
proxy_set_header Referer 'https://<IPA-HOSTNAME>/ipa/ui/';
proxy_set_header Connection 'keep-alive';
proxy_set_header Content-Encoding 'gzip, deflate, br';
proxy_pass_request_headers on;
proxy_pass_request_body on;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Request-With,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Request-With,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range, Date, Server, Connection';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Request-With,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range, Date, Server, Connection';
}
}
location /freeipa {
proxy_set_header Cookie 'ipa_session=MagBearerToken=lqXbNKwd9JjWw2Xdl8uHUk9veiHVvYppkcrXztinDML7Yur1mVcvJDkoYi7LEgOr5Rem3VuP2rHJVuGZVMSY7YHbXoNAFUfK2G3UDt7qaq%2fSAFMPa5%2balJLvi2zS0gDr4jTfQF0WRDhN9RTn8kss1ChIL9v6kjMv%2fDG%2brJ49rFA%3d';
proxy_pass https://<IPA-HOSTNAME>/ipa/session/json;
proxy_set_header Origin 'https://<IPA-HOSTNAME>';
proxy_set_header Referer 'https://<IPA-HOSTNAME>/ipa/ui/';
proxy_set_header Connection 'keep-alive';
proxy_set_header Content-Encoding 'gzip, deflate, br';
proxy_pass_request_headers on;
proxy_pass_request_body on;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Request-With,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Request-With,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range, Date, Server, Connection';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8083" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,If-Modified-Since,X-Request-With,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range, Date, Server, Connection';
}
}
}
}
答案 1 :(得分:0)
在类实例化中调用driver方法。
static {
// Ensure we have mariadb Driver in classpath
try {
Class.forName("org.mariadb.jdbc.Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
此外,您应该关闭资源。您可以使用try-with-resources来做到这一点。
public List<String> getData(String sql, Map column) {
try (
Connection conn = DriverManager.getConnection(this.url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
){
List<String> result = new ArrayList<>();
if (column.containsKey("string")) {
for (Object i : column.values()) {
while (rs.next()) {
result.add(rs.getString(String.valueOf(i)));
}
}
return result;
}
}
catch (SQLException e) {
e.printStackTrace();
}
return null;
}