jsp_project_/store/src/conn/ConnectionDatabase

本文介绍了一个Java类,用于连接SQL Server数据库并执行多种数据库操作,包括查询、更新及分页显示数据。该类提供了获取查询结果、执行SQL语句、获取订单ID和实现数据分页等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package conn;
import java.util.*;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;


public   class ConnectionDatabase {
 private Connection conn=null;
 private Statement st = null;
 private ResultSet rs = null;
 CallableStatement cmt = null;
 private String url="jdbc:jtds:sqlserver://localhost:1433/shoppingcartDataBase";
 private String user="sa";
 private String pass="";
 private int total=0;
 private String filePath;

 
    //获取查询数据
 public Vector getQueryDate(String sql)
 {
  try
  {
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   conn = DriverManager.getConnection(url,user,pass);
   st = conn.createStatement();
   rs = st.executeQuery(sql);
    
   ResultSetMetaData rsmd = rs.getMetaData();
   int column = rsmd.getColumnCount();
   Vector content = new Vector();
   while(rs.next())
   {
    Vector v = new Vector();
    for(int i=1; i <=column;i++ )
    {
     v.add(rs.getString(i));
    }
    content.add(v);
   }
   return content;
  }catch(Exception eGetQueryDate)
  {
   System.out.print("ConnectionDatabase::getQueryDate"+eGetQueryDate.getMessage());
   return null;
  }finally
  {
   try{
    if(rs!= null)
    {
     rs.close();
    }
    if(st!=null)
    {
     st.close();
    }
    if(conn != null)
    {
     conn.close();
    }
   }catch(Exception eGetQueryDateClose)
   {
    System.out.print("ConnectionDatabase::getQueryDate"+eGetQueryDateClose.getMessage());
   }
  }
 }
 //判断有没有结构集合
 public boolean isNull(String sql)
 {
  try
  {
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   conn = DriverManager.getConnection(url,user,pass);
   st = conn.createStatement();
   rs = st.executeQuery(sql);
   return rs.next();
  }catch(Exception eisNull)
  {
   System.out.print("ConnectionDatabase::isNull"+eisNull.getMessage());
   return false;
  }finally
  {
   try{
    if(rs!= null)
    {
     rs.close();
    }
    if(st!=null)
    {
     st.close();
    }
    if(conn != null)
    {
     conn.close();
    }
   }catch(Exception eisNullClose)
   {
    System.out.print("ConnectionDatabase::isNull"+eisNullClose.getMessage());
   }
  }
 }
 //执行sql的增删改synchronized
  public int updateBase(String sql)
 {
  try
  {
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   conn = DriverManager.getConnection(url,user,pass);
   st = conn.createStatement();
   return  st.executeUpdate(sql);   
  }catch(Exception eupdateBase)
  {
   System.out.print("ConnectionDatabase::updateBase"+eupdateBase.getMessage());
   return 0;
  }finally
  {
   try{
    
    if(st!=null)
    {
     st.close();
    }
    if(conn != null)
    {
     conn.close();
    }
   }catch(Exception eupdateBaseClose)
   {
    System.out.print("ConnectionDatabase::updateBase"+eupdateBaseClose.getMessage());
   }
  }
 }
 
 //获取定单ID
 public String getOrderID(String sql)
 {
  try
  {
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   conn = DriverManager.getConnection(url,user,pass);
   CallableStatement cmt=conn.prepareCall(sql);
   cmt.registerOutParameter(1,Types.VARCHAR);
   cmt.execute();
   return cmt.getString(1);
  }catch(Exception egetOrderID)
  {
   System.out.print("ConnectionDatabase::getOrderID"+egetOrderID.getMessage());
   return "";
  }finally
  {
   try{
        
    if(cmt!=null)
    {
     cmt.close();
    }
    if(conn != null)
    {
     conn.close();
    }
   }catch(Exception egetOrderID1)
   {
    System.out.print("ConnectionDatabase::updateBase"+egetOrderID1.getMessage());
   }
  }
 }
 //实现分页
 public Vector getPage(String sql,HttpServletRequest req,String pageN)//
 {
  Vector content = new Vector();
  try{
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   String sqlPart;
   int begin;
   begin = sql.indexOf("FROM");
   sqlPart ="SELECT COUNT(*) " +sql.substring(begin,sql.length());
   conn = DriverManager.getConnection(url,user,pass);
   st = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
   
   
   filePath = req.getRequestURI();
  
   rs = st.executeQuery(sqlPart);
 
   if(rs.next())
   {
    total = rs.getInt(1);
   }else
   {
    total = 0;
   }
   
   if(total>0)
   {
    rs = null;
    rs = st.executeQuery(sql); 
    ResultSetMetaData rsmt = rs.getMetaData();
    int col = rsmt.getColumnCount();   
    rs.absolute(Integer.parseInt(pageN)+1);
    rs.previous();
    int i=0;
    while(rs.next() && i<12)
    {
     Vector vcol = new Vector();
     for(int j=1;j<=col;j++)
     {
      vcol.add(rs.getString(j));
     }
     content.add(vcol);
     i++;
    }
    return content;
   }  
  }catch(Exception e1){
   e1.printStackTrace();
   return content;
  }finally{
   try{
    if(rs!=null)
    {
     rs.close();
    }
    if(st!=null)
    {
     st.close();
    }
    if(conn!=null)
    {
     conn.close();
    }
   }catch(Exception e11)
   {
    e11.printStackTrace();
   }
  }
  return null;
 }
 public int getTotal()
 {
  return total;
 }
 public String pageLenged(int pagen)
 {
  String str="";
  int prev,next;
  prev = pagen-12;
  next = pagen+12;
  if(prev >= 0)
  {
   str+="<a href="+filePath+"?offset="+prev+">上一页</a>";
  }else
  {
   str+="上一页";
  }
  str+="   ";
  if((next < total))
  {
   str+="<a href="+filePath+"?offset="+next+">下一页</a>";
  }else
  {
   str+="下一页";
  }
  return str;
 }
 public static void main(String args[])
 {
  ConnectionDatabase conn = new ConnectionDatabase();
  System.out.print(conn.getOrderID("{ call getordID(?) }"));
 }
}
 

核心需求 修复TTF字体在安卓小程序中的加载问题,确保字体文件能被正确加载和使用 帮我解决: TTF字体返回的Content-Type为application/octet-stream而非正确的font/truetype 技术环境 项目框架:ThinkPHP5 (TP5) 服务器环境:宝塔面板 运行目录:public 伪静态:已开启 字体路径:https://yinzhang.jinshelby.com/xcx/font/kaiti2.ttf 宝塔上路径是:/www/wwwroot/yinzhang.jinshelby.com/public/xcx/font/kaiti2.ttf 这是完整的Nginx主配置(nginx.conf) user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; pid /www/server/nginx/logs/nginx.pid; worker_rlimit_nofile 100000; stream { log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time'; access_log /www/wwwlogs/tcp-access.log tcp_format; error_log /www/wwwlogs/tcp-error.log; include /www/server/panel/vhost/nginx/tcp/*.conf; } events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; # 已经包含标准字体类型定义 # 修正 MIME 类型定义(移除重复项) # 手动添加字体类型映射 types { font/truetype ttf; font/opentype otf; # application/font-woff woff; # application/font-woff2 woff2; } # 确保默认类型设置正确 default_type application/octet-stream; } # 移除重复的 default_type # default_type application/font-sfnt; // 注释掉这行 default_type application/octet-stream; #include luawaf.conf; include proxy.conf; server_names_hash_bucket_size 512; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; server_tokens off; access_log off; server { listen 888; server_name phpmyadmin; index index.html index.htm index.php; root /www/server/phpmyadmin; location ~ /tmp/ { return 403; } #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~* ^/xcx/font/.*\.(ttf|otf|woff|woff2)$ { root /www/wwwroot/yinzhang.jinshelby.com/public; # 添加详细日志 access_log /www/wwwlogs/font_access.log; error_log /www/wwwlogs/font_error.log notice; # # 强制设置正确的 Content-Type(重点) # if ($uri ~ \.ttf$) { # add_header Content-Type "font/truetype" always; # } # if ($uri ~ \.otf$) { # add_header Content-Type "font/opentype" always; # } # if ($uri ~ \.woff$) { # add_header Content-Type "font/woff" always; # } # if ($uri ~ \.woff2$) { # add_header Content-Type "font/woff2" always; # } # CORS 支持 if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Range'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Range' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; # 缓存控制 expires 1y; add_header Cache-Control "public, max-age=31536000, immutable"; # 字节范围支持(安卓必需) add_header Accept-Ranges bytes; try_files $uri = 404; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; } 这是 网站的配置文件内容: server { listen 80; listen 443 ssl http2 ; server_name yinzhang.jinshelby.com; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/yinzhang.jinshelby.com/public; #CERT-APPLY-CHECK--START # 用于SSL证书申请时的文件验证相关配置 -- 请勿删除 include /www/server/panel/vhost/nginx/well-known/yinzhang.jinshelby.com.conf; #CERT-APPLY-CHECK--END #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #HTTP_TO_HTTPS_START if ($server_port !~ 443){ rewrite ^(/.*)$ https://$host$1 permanent; } #HTTP_TO_HTTPS_END ssl_certificate /www/server/panel/vhost/cert/yinzhang.jinshelby.com/fullchain.pem; ssl_certificate_key /www/server/panel/vhost/cert/yinzhang.jinshelby.com/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_tickets on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; add_header Strict-Transport-Security "max-age=31536000"; error_page 497 https://$host$request_uri; #SSL-END #引用重定向规则,注释后配置的重定向代理将无效 include /www/server/panel/vhost/nginx/redirect/yinzhang.jinshelby.com/*.conf; #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-74.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/yinzhang.jinshelby.com.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; } #禁止在证书验证目录放入敏感文件 if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) { return 403; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ttf)$ { expires 30d; error_log /dev/null; access_log /dev/null; } location ~ .*\.(js|css)?$ { expires 12h; error_log /dev/null; access_log /dev/null; } access_log /www/wwwlogs/yinzhang.jinshelby.com.log; error_log /www/wwwlogs/yinzhang.jinshelby.com.error.log; }
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值