Basic Java Web tools-BaseDao and javawebbasedao

Source: Internet
Author: User

Basic Java Web tools-BaseDao and javawebbasedao

 1 package dao; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 public class BaseDao {11     private static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=StudentManageSys";12     private static String USER = "sa";13     private static String PWD = "sa";14     protected Connection conn = null;15     protected PreparedStatement pstmt = null;16 17     static {18         try {19             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");20         } catch (ClassNotFoundException e) {21             e.printStackTrace();22         }23     }24 25     protected Connection getConnection() {26         try {27             return conn == null ? DriverManager.getConnection(URL, USER, PWD) : conn;28         } catch (SQLException e) {29             e.printStackTrace();30             return null;31         }32     }33 34     protected void closeAll(Connection conn, Statement stmt, ResultSet rs) {35         try {36             if (conn != null)37                 conn.close();38             if (stmt != null)39                 stmt.close();40             if (rs != null)41                 rs.close();42         } catch (SQLException e) {43             e.printStackTrace();44         }45     }46 47     protected Object execute(String sql, Object... objs) {48         handle(sql, objs);49         try {50             boolean flag = pstmt.execute();51             return flag ? pstmt.getResultSet() : pstmt.getUpdateCount();52         } catch (SQLException e) {53             e.printStackTrace();54             return null;55         }56     }57 58     private void handle(String sql, Object[] objs) {59         conn = getConnection();60         try {61             pstmt = conn.prepareStatement(sql);62             if (objs != null) {63                 for (int i = 0; i < objs.length; i++) {64                     pstmt.setObject(i + 1, objs[i]);65                 }66             }67         } catch (SQLException e) {68             e.printStackTrace();69         }70     }71 }

I wrote a simple tool class when I was studying Servlet in the second semester. After graduation, I improved my skills and technology is still at the cainiao level. I can only change it to this type. I wish you every day better!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.