preparedstatement
背景:
在使用Preparedstatement执行setString(4, "我是中文");后,通过debug发现中文变成了???;这个肯定是编码问题,然后通过字符串和字节码进行编码转换也没能够解决这个问题;最后在网上搜到一篇文章提到,在连接mysql数据的时候需要制定编码:?characterEncoding=utf8
解决:
public String getMysqlUrl() {
return "jdbc:mysql://" + ip + ":" + port + "/" + sid+"?characterEncoding=utf8";
}
下面是整个测试代码:
package com.scu.tgm.test;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.*;
public class TestMysql {
private static final String oracle = "oracle";
private static final String SQL_SERVER = "mssql";
private static final String SYBASE = "sybase";
private static final String MYSQL = "mysql";
private static final String MYSQL_DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String ORACLE_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
private static final String SQLSERVER_DRIVER_CLASS = "net.sourceforge.jtds.jdbc.Driver";
private static final String SYBASE_DRIVER_CLASS = "com.sybase.jdbc3.jdbc.SybDriver";
private String type ="mysql";
private String ip ="localhost";
private String port ="3306";
private String user ="root";
private String sid ="tgmtest";
private String password ="root";
private Connection connection;
public static void main(String[] str) throws UnsupportedEncodingException{
TestMysql testMysql=new TestMysql();
String sql="Insert into sms_outbox (sismsid, extcode, destaddr, messagecontent,reqdeliveryreport,msgfmt,sendmethod,requesttime,APPlicationid) VALUES(?,?,?,?,?,?,?,?,?)";
testMysql.createConnect();
try {
testMysql.executeInsert(sql,"123;44","八嘎雅路");
} catch (SQLException e) {
e.printstacktrace();
}
System.out.println();
}
/**
* Insert into t_user
* (sismsid, extcode, destaddr, messagecontent,reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid)
* VALUES(?,?,?,?,?,?,?,?,?)
* @param sql
* @return
* @throws SQLException
* @throws UnsupportedEncodingException
*/
public void executeInsert(String sql,String phNumbers,String messagecontent) throws SQLException, UnsupportedEncodingException {
PreparedStatement ps = connection.preparestatement(sql);
ps.setString(1, UUID.randomUUID().toString());
ps.setString(2, "");
ps.setString(3, phNumbers);
ps.setString(4, messagecontent);
ps.setByte(5, (byte) 1);
ps.setByte(6, (byte) 15);
ps.setByte(7,(byte) 0);
ps.settimestamp(8, new Timestamp((new java.util.Date()).getTime()));
ps.setString(9, "dxcs");
ps.executeUpdate();
}
public void createConnect() {
if (ORACLE.equalsignorecase(type)) {
String fornameCalss = ORACLE_DRIVER_CLASS;
String url = getOracleUrl();
connection= createConnection(fornameCalss, url,user,password);
} else if (SQL_SERVER.equalsIgnoreCase(type)) {
String fornameCalss = SQLSERVER_DRIVER_CLASS;
String url = getSqlserverUrl();
connection= createConnection(fornameCalss, url,user,password);
} else if (SYBASE.equalsIgnoreCase(type)) {
String fornameCalss = SYBASE_DRIVER_CLASS;
String url = getSybaseUrl();
connection= createConnection(fornameCalss, url,user,password);
} else if (MYSQL.equalsIgnoreCase(type)) {
String fornameCalss = MYSQL_DRIVER_CLASS;
String url = getMysqlUrl();
connection= createConnection(fornameCalss, url,user,password);
}else {
}
}
private Connection createConnection(String fornameCalss, String url,String user, String password) {
Connection connection = null;
try {
class.forname(fornameCalss);
connection = DriverManager.getConnection(url, user,password);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return connection;
}
public String getSybaseUrl() {
return "jdbc:sybase:Tds:" + ip + ":" + port + "/" + sid;
}
public String getSqlserverUrl() {
return "jdbc:jtds:sqlserver://" + ip + ":" + port + "/" + sid;
}
public String getOracleUrl() {
return "jdbc:oracle:thin:@" + ip + ":" + port + ":" + sid;
}
public String getMysqlUrl() {
return "jdbc:mysql://" + ip + ":" + port + "/" + sid+"?characterEncoding=utf8";
}
}
文章最后发布于: 2018-10-18 12:58:10
相关阅读
想要买到价格又便宜质量又好的持平,买家会选择一些其他的渠道,像是内购平台、优惠券群等等,很多朋友对于内购平台不是很了解,不知道该
iphone未越狱使用iphone九宫格输入法 搜狗输入法使用
搜狗输入板除了支持短信和邮件之外,还多了微博和微信的文字转换。在搜狗输入板中编辑好文字后,可以通过点击界面顶端的短信、邮件、
前言 android的线程池主要有四个: newSingleThreadExecutor:单线程池,同时只有一个线程在跑。newCachedThreadPool() :回收型线程池
1.前言 本实例需要用到的python包有requests、PrettyTable(用于打印展示成表格形式) pip install requests pip install pretty
ViewState就是页面中的名字叫做__VIEWSTATE的隐藏控件,它使我们页面体积膨胀,不利于页面的seo优化;页面返送间都要携带更多的大量的