python utf8_肿么在Python里使用UTF-8编码
String sql ="insert into userinfo(name,pwd) values(?,?)";
PreparedStatement pst=getConnection().prepareStatement(sql);
pst.setString(1,"小明");
pst.setString(2,"123");
pst.executeUpdate();
小伙子給你寫了一個通用的增刪改查的工具類,你那樣寫太麻煩
資源文件jdbc.properties
jdbc.Driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/xxxjdbc.username=rootjdbc.password=root
package com.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Properties;import org.apache.commons.beanutils.BeanUtils;import org.apache.log4j.Logger;public class BaseDao { private static String DRIVDR; private static String URL; private static String USER; private static String PWD; private static Connection connection; private static Properties properties = new Properties(); private static Logger logger = Logger.getLogger(BaseDao.class); private static PreparedStatement pst; private static ResultSet rs; private static final String CLASS_NOT_EXCEPTION = "驅動加載失敗"; static { try { properties.load(BaseDao.class.getResourceAsStream("/jdbc.properties")); DRIVDR = properties.getProperty("jdbc.Driver"); URL = properties.getProperty("jdbc.url"); USER = properties.getProperty("jdbc.username"); PWD = properties.getProperty("jdbc.password"); Class.forName(DRIVDR); } catch (Exception e) { logger.debug(CLASS_NOT_EXCEPTION + e.getMessage()); } } /** * 通用的增刪改 * * @param sql * @param args * @return */ public static int executeCommand(String sql, Object... args) { int m = 0; try { initPreparedStatement(sql, args); m = pst.executeUpdate(); } catch (Exception e) { logger.debug("執行增、刪、該。錯誤。。請檢查preparedStatement參數。。。"+e.getMessage()); } finally { closeAll(null, pst, connection); } return m; } private static PreparedStatement initPreparedStatement(String sql,Object...args){ try { pst=getConnection().prepareStatement(sql); if(args!=null){ for(int i=0;i T findById(String sql,Class clazz,Object...args){ T t = null; try { initPreparedStatement(sql, args); rs = pst.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); // 以上的代碼:獲取元數據(各個字段的數據類型) int count = metaData.getColumnCount(); // 獲取字段的數量 if(rs.next()) { try { t = clazz.newInstance();// 利用反射自動創建對象的類型的對象 User.class User // u=new User(); for (int i = 1; i <= count; i++) { BeanUtils.copyProperty(t, metaData.getColumnName(i), rs.getObject(i)); // 自動獲取各個字段的名稱并獲取該字段的值 } } catch (Exception e) { logger.debug("查詢單個對象,錯誤。。請檢查preparedStatement參數。。。。"+e.getMessage()); } } } catch (SQLException e) { logger.debug("查詢失敗。。。。。" + e.getMessage()); } return t; } /** * 通用的查詢 * * @param sql * @param clazz * @param args * @return */ public static List findAll(String sql, Class clazz, Object... args) { List list = new ArrayList(100); T t = null; try { initPreparedStatement(sql, args); rs = pst.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); // 以上的代碼:獲取元數據(各個字段的數據類型) int count = metaData.getColumnCount(); // 獲取字段的數量 while (rs.next()) { try { t = clazz.newInstance();// 利用反射自動創建對象的類型的對象 User.class User // u=new User(); for (int i = 1; i <= count; i++) { BeanUtils.copyProperty(t, metaData.getColumnName(i), rs.getObject(i)); // 自動獲取各個字段的名稱并獲取該字段的值 } } catch (Exception e) { logger.debug("查詢集合,錯誤。。。。。"+e.getMessage()); } list.add(t);// 將對象添加到集合中 } } catch (SQLException e) { logger.debug("查詢失敗。。。。。" + e.getMessage()); } return list; } public static Connection getConnection() { try { if (connection == null || connection.isClosed()) { connection = DriverManager.getConnection(URL, USER, PWD); } } catch (SQLException e) { logger.debug("獲取connection失敗,請檢查配置文件!" + e.getMessage()); } return connection; } public static void closeAll(ResultSet rs, PreparedStatement pst, Connection conn) { if (rs != null) try { rs.close(); } catch (SQLException e) { logger.debug("關閉ResultSet錯誤。。。。。" + e.getMessage()); } if (pst != null) try { pst.close(); } catch (SQLException e) { logger.debug("關閉PreparedStatement錯誤。。。。。" + e.getMessage()); } if (conn != null) closeConnection(conn); } private static void closeConnection(Connection conn) { try { if (!conn.isClosed()) { conn.close(); } } catch (SQLException e) { logger.debug("關閉Connection錯誤。。。。。" + e.getMessage()); } finally { conn = null; } } public static void main(String[] args) { System.out.println(BaseDao.getConnection()); }}
取消
評論
總結
以上是生活随笔為你收集整理的python utf8_肿么在Python里使用UTF-8编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++字符加密_linux安全Linux
- 下一篇: python3.9特性_Python3.