Java基礎--JDBC-HashMap__Java

來源:互聯網
上載者:User

Java基礎--JDBC-HashMap

package com.JDBCTest;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class QuerySession {private static final String url = "jdbc:mysql://119.254.106.50:3309/test?useUnicode=true&characterEncoding=utf8";private static final String user = "rw_all_db";private static final String password = "rw_all_db";/** * 擷取串連 */public static Connection getConnection() {try {Class.forName("com.mysql.jdbc.Driver");//靜態代碼塊執行個體化一個Driver對象Connection connection = DriverManager.getConnection(url, user, password);return connection;} catch (Exception e) {e.printStackTrace();return null;}}public static void closeConn(Connection connection,PreparedStatement statement,ResultSet rs) {try {if(rs!=null)rs.close();if(statement!=null)statement.close();if(connection!=null)connection.close();} catch (Exception e) {e.printStackTrace();}}public static List<HashMap<String, Object>> findData(String sql) {Connection connection = null;PreparedStatement statement = null;ResultSet rs = null;List<HashMap<String, Object>> users = null;try {connection = getConnection();statement = connection.prepareStatement(sql);rs = statement.executeQuery();users = new ArrayList<>();HashMap<String, Object> hashMap = null;while (rs.next()) {hashMap = new HashMap<>();hashMap.put("id", rs.getInt("id"));hashMap.put("username", rs.getString("username"));hashMap.put("address", rs.getString("address"));hashMap.put("birthday", rs.getString("birthday"));users.add(hashMap);}return users;} catch (Exception e) {e.printStackTrace();return null;} finally{closeConn(connection, statement, rs);}}public static List<HashMap<String, Object>> searchData(String sql2,String address,String username) {Connection connection = null;PreparedStatement statement = null;ResultSet rs = null;List<HashMap<String, Object>> users = null;try {connection = getConnection();statement = connection.prepareStatement(sql2);statement.setString(1, address);statement.setString(2, "%"+username+"%");rs = statement.executeQuery();users = new ArrayList<>();HashMap<String, Object> hashMap = null;while (rs.next()) {hashMap = new HashMap<>();hashMap.put("id", rs.getInt("id"));hashMap.put("username", rs.getString("username"));hashMap.put("address", rs.getString("address"));hashMap.put("birthday", rs.getString("birthday"));users.add(hashMap);}return users;} catch (Exception e) {e.printStackTrace();return null;} finally{closeConn(connection, statement, rs);}}public static HashMap<String, Object> findDataPK(String sql1,Integer id) {Connection connection = null;PreparedStatement statement = null;ResultSet rs = null;try {connection = getConnection();statement = connection.prepareStatement(sql1);statement.setInt(1, id);rs = statement.executeQuery();HashMap<String, Object> hashMap = null;if (rs.next()) {hashMap = new HashMap<>();hashMap.put("id", rs.getInt("id"));hashMap.put("username", rs.getString("username"));hashMap.put("address", rs.getString("address"));hashMap.put("birthday", rs.getString("birthday"));}return hashMap;} catch (Exception e) {e.printStackTrace();return null;} finally{closeConn(connection, statement, rs);}}public static void main(String[] args) {//String sql = "SELECT id,username,address,birthday FROM user;";//List<HashMap<String, Object>> users = findData(sql);//for (HashMap<String, Object> hashMap : users) {//System.out.println(hashMap.get("id")+"=>"+hashMap.get("username")+"=>"+hashMap.get("address")+"=>"+hashMap.get("birthday"));//}////String sql1 = "SELECT id,username,address,birthday FROM user where id = ?";//HashMap<String, Object> hashMap = findDataPK(sql1, 3);//if (hashMap != null) {//System.out.println(hashMap.get("id")+"=>"+hashMap.get("username")+"=>"+hashMap.get("address")+"=>"+hashMap.get("birthday"));//}String sql2 = "SELECT id,username,address,birthday FROM user where address = ? and username like ?";List<HashMap<String, Object>> users = searchData(sql2, "北京", "a");for (HashMap<String, Object> hashMap : users) {System.out.println(hashMap.get("id")+"=>"+hashMap.get("username")+"=>"+hashMap.get("address")+"=>"+hashMap.get("birthday"));}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.