• 关键字中mysql数据库查询条件带中文无结果解决办法


     1 package keyword;
     2 
     3 import java.io.UnsupportedEncodingException;
     4 import java.sql.Connection;
     5 import java.sql.DriverManager;
     6 import java.sql.PreparedStatement;
     7 import java.sql.ResultSet;
     8 import java.sql.SQLException;
     9 
    10 import cn.suning.automation.execute.Parameter;
    11 import cn.suning.automation.param.Reference;
    12 
    13 public class MySQLConnection {
    14     private static final String DBDRIVER = "com.mysql.jdbc.Driver";
    15     private static final String DBURL = "jdbc:mysql://localhost:3306/dbname"
    16             + "user=root@password=pwd@useUnicode=true&characterEncoding=UTF8";
    17     private static final String URL = "jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF8"; //如果不指定字符集则在封装的关键字里查询不到结果
    18     private static final String DBUSER = "root";
    19     private static final String PASSWORD = "pwd";
    20     private Connection conn = null;
    21 
    22     public MySQLConnection() {
    23         try {
    24             Class.forName(DBDRIVER);
    25             //System.out.println("成功加载MySQL驱动!");
    26             this.conn = DriverManager.getConnection(URL, DBUSER, PASSWORD);
    27 //            this.conn=DriverManager.getConnection(DBURL);
    28         } catch (Exception e) {
    29             e.printStackTrace();
    30         }
    31     }
    32 
    33     public Connection getConnection() {
    34         return this.conn;
    35     }
    36 
    37     public void close() {
    38         if (this.conn != null) {
    39             try {
    40                 this.conn.close();
    41             } catch (SQLException e) {
    42                 e.printStackTrace();
    43             }
    44         }
    45     }
    46     
    47     public static void encodedString(Parameter poParam) throws UnsupportedEncodingException {
    48         String sResult=String.valueOf(getValue());
    49         
    50         Reference<String> oResult=Reference.refer(poParam.getString("result"));
    51         oResult.setValue(poParam, sResult);
    52     }
    53     
    54     public static int getValue() {
    55         int value=0;
    56         MySQLConnection mySQLConnection = new MySQLConnection();
    57         Connection conn = mySQLConnection.getConnection();
    58         //String sql = "INSERT INTO student(name) VALUES(?)";
    59         String sql="SELECT colName FROM tableName where colName2='中文'";
    60         try {
    61             PreparedStatement statement = conn.prepareStatement(sql);
    62             // ResultSet resultSet=statement.executeQuery();
    63             //statement.setString(1, "赵六子");
    64            // System.out.println(statement.executeUpdate());
    65             ResultSet rs=statement.executeQuery(sql);
    66             
    67             while (rs.next()){                
    68                 System.out.print(rs.getInt(1) + "	");
    69 //                System.out.print(rs.getInt(2) + "	");
    70 //                System.out.print(rs.getString(3) + "	");
    71                 System.out.println();
    72                 value=rs.getInt(1);
    73             }
    74                 
    75             rs.close();
    76             
    77             conn.close();
    78         } catch (SQLException e) {
    79             e.printStackTrace();
    80         }
    81         return value;
    82     }
    83     
    84     public static void main(String[]args) {
    85         System.out.println(getValue());
    86     }
    87 }
  • 相关阅读:
    20145215《信息安全系统设计基础》第三周学习总结
    20145215《信息安全系统设计基础》第二周学习总结
    Ubuntu下git的安装与使用
    20145215《信息安全系统设计基础》第一周学习总结
    20145215《信息安全系统设计基础》第0周学习总结
    第一周Web类WriteUp
    20145215《Java程序设计》课程总结
    20145215《Java程序设计》第10周学习总结
    python字符串基本方法
    python数据类型
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/7767021.html
Copyright © 2020-2023  润新知