JDBC杩炴帴MySQL鎿嶄綔姝ラ

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC杩炴帴MySQL鎿嶄綔姝ラ相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/state' title='state'>state   cal   鍙橀噺   count   static   aik   str   

package testJDBC;

import java.sql.*;

/**

  • @Author 灏忕尓涓婂ぉ
  • @Email zhuhuaikuan@gmail.com
  • @Data 2019/8/28 14:27
  • @Version V1.0
  • @description JDBC 鏌ヨ鏁版嵁搴撶ず渚?br/>*/
    public class JDBCdemo1

    public static void main(String[] args)
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;
    //1.娉ㄥ唽鏁版嵁搴撻┍鍔?br/>try

        Class.forName("com.mysql.jdbc.Driver");
        //鍔犺浇涓€涓被鍒板唴瀛樹腑,璇ョ被涓湁涓€闈欐€佷唬鐮佸潡,璇ヤ唬鐮佸潡涓湁娉ㄥ唽椹卞姩鐨勪唬鐮?    //闈欐€佷唬鐮佸潡浼唬鐮佸涓嬶細DriverManager.registerDriver( new Driver() )
    
        //2.鑾峰彇鏁版嵁搴撹繛鎺?    String url = "jdbc:mysql://localhost:3306/" +
                "jt_db" +
                "?characterEncoding=utf-8";
        /**
         * jdbc:mysql:                   鍗忚鍚?     * localhost:3306               涓绘満鍦板潃鍙婄鍙?     * jt_db                        鏁版嵁搴撳悕
         * ?characterEncoding=utf-8     锛?鍙傛暟
         */
    
        String user = "root";           //鏁版嵁搴撹繛鎺ョ敤鎴峰悕
        String passWord = "123456";     //鏁版嵁搴撹繛鎺ュ瘑鐮?
       conn = DriverManager.getConnection(url, user, passWord);
    
        //3.鑾峰彇浼犺緭鍣?
        statement = conn.createStatement();
    
        /**
         * Statement s = con.createStatement();
         * PreparedStatement p = con.preparedStatement();
         *
         *PreparedStatement鏄疭tatement鐨勫瓙绫?     * 鍙互浣跨敤鍗犱綅绗︼紝鏄缂栬瘧鐨勶紝鎵瑰鐞嗘瘮Statement鏁堢巼楂?     *
         * 渚?
         *  String sql = "update user set password=? where username=?";
         *   preparedStatement = conn.prepareStatement(sql);
         *   //preparedStatement.setString(绗琋涓棶鍙?浠h〃鐨勫彉閲?;
         *   preparedStatement.setString(1,password);
         *   preparedStatement.setString(2,username);
         *   preparedStatement.executeUpdate();
         *
         */
    
        //4.鎵цsql璇彞,杩斿洖鎵ц缁撴灉
    
        String sql = "select * from account";
        /**
         * executeQuery(); 鐢ㄤ簬浜х敓鍗曚釜缁撴灉闆嗙殑璇彞,琚敤鏉ユ墽琛?SELECT 璇彞,杩斿洖鍊兼槸涓€涓猂esultSet缁撴灉闆嗗璞?鍏朵腑鍖呮嫭sql鏌ヨ鐨勭粨鏋?     * executeUpdate(); 鐢ㄤ簬鎵ц INSERT銆乁PDATE 鎴?DELETE 璇彞浠ュ強 SQL DDL锛堟暟鎹畾涔夎瑷€锛夎鍙?杩斿洖涓€涓猧nt鍊?璁板綍鍙楀奖鍝嶈褰曡鏁?     */
        resultSet = statement.executeQuery(sql);
    
        //5.澶勭悊缁撴灉
        /**
         * ResultSet 缁撴灉闆嗗璞?     *  ResultSet 鏄墽琛屾煡璇㈢被鐨剆ql璇彞鍚庤繑鍥炵殑缁撴灉闆嗗璞?鍏朵腑鍖呮嫭sql鏌ヨ鐨勭粨鏋?     *  a.閬嶅巻鏁版嵁琛岀殑鏂规硶
         *  next() 鈥?浣挎寚鍚戞暟鎹鐨勭储寮曞悜涓嬬Щ鍔ㄤ竴琛?     *  b.鑾峰彇鏁版嵁鐨勬柟娉?     *  getInt(int columnIndex)
         *  getInt(String columnLable)
         *  getString(int columnIndex)
         *  getString(String columnLable)
         *  getDouble(int columnIndex)
         *  getDouble(String columnLable)
         *  getObject(int columnIndex)
         *  getObject(String columnLable)
         */
        while (resultSet.next()) 
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            double money = resultSet.getDouble("money");
            System.out.println(id + ":" + name + ":" + money);
        
    catch (Exception e)
        e.printStackTrace();
    finally 
    
     //6.閲婃斁璧勬簮
        //瓒婃櫄鑾峰彇瓒婂厛閲婃斁锛侊紒
        //鏈€濂藉皢閲婃斁璧勬簮浠g爜鏀惧湪finally閲? if(resultSet!=null) 
         try 
             resultSet.close();
          catch (SQLException e) 
             e.printStackTrace();
          finally 
             resultSet = null;
         
     
        if(statement!=null) 
            try 
                statement.close();
             catch (SQLException e) 
                e.printStackTrace();
             finally 
                statement = null;
            
        
        if(conn!=null) 
            try 
                conn.close();
             catch (SQLException e) 
                e.printStackTrace();
             finally 
                conn = null;
            
        
    
    


以上是关于JDBC杩炴帴MySQL鎿嶄綔姝ラ的主要内容,如果未能解决你的问题,请参考以下文章

koa,express,node閫氱敤鏂规硶杩炴帴MongoDB

MySQL涔嬪垵浣跨敤

鏁版嵁搴撶瑪璁?馃搾 MySQL鍩虹鎿嶄綔鍛戒护 鍒嗙被娓呮櫚

20210622锝滄妧鏈垎浜綔mysql 杩滅▼鏃犳硶杩炴帴澶勭悊

MySQL-鍩烘湰鎿嶄綔

MySQL鐨凷QL璇彞 - 鏁版嵁鎿嶄綔璇彞锛?锛? DO 璇彞