日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python utf8_肿么在Python里使用UTF-8编码

發布時間:2025/3/19 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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编码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。