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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql的util_JDBC连接mysql工具类Util供大家参考

發布時間:2023/12/10 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql的util_JDBC连接mysql工具类Util供大家参考 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

> list = new ArrayList>();

//靜態代碼塊,在程序編譯的時候執行

static {

//創建Properties對象

Properties properties = new Properties();

//獲取文件輸入流

InputStream is = JDBCUtil_cj.class.getClassLoader().getResourceAsStream("jdbc.properties");

try {

//加載輸入流

properties.load(is);

is.close();

//加載mysql驅動

Class.forName(properties.getProperty("driver"));

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

//關閉資源

public static void closeRes(Connection conn,PreparedStatement ps,ResultSet rs) {

try {

if(rs != null){

rs.close();

}

if(ps != null){

ps.close();

}

if(conn != null){

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

//獲取連接

public static Connection getConnection(){

Connection conn = null;

Properties properties = new Properties();

InputStream is = JDBCUtil_cj.class.getClassLoader().getResourceAsStream("jdbc.properties");

try {

properties.load(is);

is.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

conn = DriverManager.getConnection(

properties.getProperty("url"),

properties.getProperty("username"),

properties.getProperty("password"));

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static void main(String[] args) {

Connection connection = getConnection();

String sql = "select * from dept where deptno = ?";

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setInt(1,10);

resultSet = preparedStatement.executeQuery();

ResultSetMetaData md = resultSet.getMetaData(); //獲得結果集結構信息,元數據

int columnCount = md.getColumnCount(); //獲得列數

while (resultSet.next()) {

MaprowData = new HashMap();

for (int i = 1; i <= columnCount; i++) {

rowData.put(md.getColumnName(i), resultSet.getObject(i));

}

JDBCUtil_cj cj = new JDBCUtil_cj();

cj.list.add(rowData);

for (Map.Entryentry : rowData.entrySet()) {

System.out.println(entry.getKey()+"==="+entry.getValue());

}

System.out.println(rowData);

System.out.println(cj.list);

}

} catch (SQLException e) {

e.printStackTrace();

}

JDBCUtil_cj.closeRes(connection,preparedStatement,resultSet);

}

}

總結

以上是生活随笔為你收集整理的mysql的util_JDBC连接mysql工具类Util供大家参考的全部內容,希望文章能夠幫你解決所遇到的問題。

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