JAVA操作mysql

來源:互聯網
上載者:User

標籤:blog   io   os   java   ar   for   div   sp   cti   

所需jar包:mysql-connector-java.jar

代碼:

import java.sql.*;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class ConnectMySQL {public static String driver = "com.mysql.jdbc.Driver";private static String host;private static String user;private static String pwd;private static Connection conn = null;private static Statement stmt = null;public static void connect(String host, String user, String pwd) {ConnectMySQL.close();ConnectMySQL.host = host;ConnectMySQL.user = user;ConnectMySQL.pwd = pwd;}public static synchronized List<HashMap<String, String>> query(String sql) {return ConnectMySQL.result(sql);}public static synchronized void close() {try {if (stmt != null) {stmt.close();stmt = null;}if (conn != null) {conn.close();conn = null;}} catch (SQLException e) {e.printStackTrace();}}private static void connectMySQL() {try {Class.forName(driver).newInstance();conn = (Connection) DriverManager.getConnection("jdbc:mysql://"+ host + "?useUnicode=true&characterEncoding=UTF8", user,pwd);} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}private static void statement() {if (conn == null) {ConnectMySQL.connectMySQL();}try {stmt = (Statement) conn.createStatement();} catch (SQLException e) {e.printStackTrace();}}private static ResultSet resultSet(String sql) {ResultSet rs = null;if (stmt == null) {ConnectMySQL.statement();}try {rs = stmt.executeQuery(sql);} catch (SQLException e) {e.printStackTrace();}return rs;}private static List<HashMap<String, String>> result(String sql) {ResultSet rs = ConnectMySQL.resultSet(sql);List<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();try {ResultSetMetaData md = rs.getMetaData();int cc = md.getColumnCount();while (rs.next()) {HashMap<String, String> columnMap = new HashMap<String, String>();for (int i = 1; i <= cc; i++) {columnMap.put(md.getColumnName(i), rs.getString(i));}result.add(columnMap);}} catch (SQLException e) {e.printStackTrace();}return result;}public static void main(String[] args) throws SQLException {ConnectMySQL.connect("192.168.1.1/test", "test", "test");List<HashMap<String, String>> rs = ConnectMySQL.query("SELECT * from test");System.out.println(rs.get(0).get("test"));ConnectMySQL.close();}}

 

JAVA操作mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.