JDBC鍒濆锛孌AO妯″紡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC鍒濆锛孌AO妯″紡相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/query' title='query'>query   lips   com   array   server   try   close   鏁版嵁搴撹〃   div   

涓€銆佺敤eclipse杩炴帴mysql鏁版嵁搴擄細

瑕佸厛娣诲姞涓€涓枃浠讹細https://www.cnblogs.com/caiwenjing/p/8079227.html

浜屻€佸紑濮嬪啓杩炴帴鏁版嵁搴撶殑浠g爜锛?/span>

鍥犱负杩炴帴鏁版嵁搴撶殑鎿嶄綔涓€鑸槸鍏敤鐨勶紝鎵€浠ュ啓鍦ㄥ彨鍋歶til鐨勫寘閲?/span>

鍍忓疄浣撶被涓€鑸啓鍦ㄥ彨鍋歟ntity鍖呴噷闈?/span>

娴嬭瘯绫伙紙JUnit閲岃瑙h繃鎬庝箞浣跨敤锛夊啓鍦ㄥ彨鍋歵est鐨勫寘閲岄潰

 

package com.util;

import java.sql.*;

public class ConnectionUtil 
    /**
     * 绗竴姝ワ細鍔犺浇椹卞姩
     * 绗簩姝ワ細閾炬帴鏁版嵁搴?
     * 绗笁姝ワ細涓€瀹氳鍏抽棴娴?
     * 绗洓姝ワ細娴嬭瘯鏄惁杩炴帴鎴愬姛
     */
    private static String DRIVER = "com.mysql.cj.jdbc.Driver";         // 鏁版嵁搴撻┍鍔?/span>
    private static String URL = "jdbc:mysql://localhost:3306/xxxy?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";    // 璁块棶鏁版嵁搴撹矾寰?
                                                     //xxxy 鏄垜杩炴帴鐨勬暟鎹簱鍚嶇О
    private static String NAME = "root";                            // 鏁版嵁搴撶敤鎴峰悕
    private static String PASSWORD = "root";                        // 鏁版嵁搴撳瘑鐮?/span>
 
 
    public static Connection getConnection() 
        Connection connection = null;
        try 
            // 鍔犺浇椹卞姩
            Class.forName(DRIVER);
            // 杩炴帴鏁版嵁搴?
            connection = DriverManager.getConnection(URL, NAME, PASSWORD);
            return connection;
         catch (Exception e) 
            return null;
        
    
 
 
    // 鍏抽棴娴?/span>
    public static void closeConnection(Connection connection) 
        try 
            connection.close();
         catch (Exception e) 
            e.printStackTrace();
        
    
 
 
    public static void closeStatement(Statement statement) 
        try 
            statement.close();
         catch (Exception e) 
            e.printStackTrace();
        
    
 
 
    public static void closePreparedStatement(PreparedStatement pStatement) 
        try 
            pStatement.close();
         catch (Exception e) 
            e.printStackTrace();
        
    
 
 
    public static void closeResultSet(ResultSet rs) 
        try 
            rs.close();
         catch (Exception e) 
            e.printStackTrace();
        
    
    // 娴嬭瘯鏁版嵁搴撴槸鍚﹂摼鎺ユ垚鍔?/span>
    /*public static void main(String[] args) 
        System.out.println("good"+getConnection());
    */

涓夈€佸啓鎴怐AO妯″紡鐨勫鍒犳敼鏌ユ搷浣滐紙鍐欐垚杩欐牱浠g爜鏇村姞瑙勮寖锛侊級

銆€銆€DAO妯″紡鍙互鎶婂疄鐜版暟鎹簱琛ㄧ殑鎿嶄綔杞寲涓哄JAVA绫荤殑鎿嶄綔

/* read all data */
    public static List<Users> readDao() 
        String sql = "select * from Journalism";
        Connection connect = null;
        PreparedStatement ptmt = null;
        ResultSet rs = null;
        List<Users> lists = new ArrayList<Users>();
        try 
            connect = ConnectionUtil.getConnection();
            ptmt = connect.prepareStatement(sql);
            rs = ptmt.executeQuery();

            while (rs.next()) 

                Users user = new Users();
                user.setType(rs.getString("type"));
                user.setContent(rs.getString("content"));
                user.setTitle(rs.getString("title"));
                user.setAuthor(rs.getString("author"));
                user.setId(rs.getString("id"));
                user.setImg(rs.getString("img"));
                user.setToday(rs.getTimestamp("today"));

                lists.add(user);
            
            return lists;

         catch (Exception e) 
            e.printStackTrace();
            return null;
         finally 
            ConnectionUtil.closeResultSet(rs);
            ConnectionUtil.closePreparedStatement(ptmt);
            ConnectionUtil.closeConnection(connect);
        
    

    
    /* delete the data */
    public static boolean deleteDao(Users user) 
        String sql = "delete from Journalism where id = ?";
        Connection connect = null;
        PreparedStatement ptmt = null;
        try 
            connect = ConnectionUtil.getConnection();
            ptmt = connect.prepareStatement(sql);
            ptmt.setString(1, user.getId());
            int i = ptmt.executeUpdate();
            return i > 0 ? true : false;
         catch (Exception e) 
            e.printStackTrace();
            return false;
         finally 
            ConnectionUtil.closePreparedStatement(ptmt);
            ConnectionUtil.closeConnection(connect);
        
    

 

以上是关于JDBC鍒濆锛孌AO妯″紡的主要内容,如果未能解决你的问题,请参考以下文章

璁捐妯″紡璇剧▼ 璁捐妯″紡绮捐 10-2 澶栬妯″紡coding

璁捐妯″紡

璁捐妯″紡锛歴tate妯″紡

璁捐妯″紡@绗?5绔狅細浠g悊妯″紡

澶栬妯″紡

璁捐妯″紡涔嬬粨鏋勫瀷--瑁呴グ妯″紡