• 2020年11月6日


    今天学习了JDBC在Java web中的应用,没有遇到问题,明天计划看一看考试的内容。

    封装对象的数据库相关操作;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class DBUtil {
        private static String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8";
        private static String user = "root";
        private static String password = "789987";
        private static String jdbcName="com.mysql.jdbc.Driver";
        private Connection con=null;
        public static  Connection getConnection() {
            Connection con=null;
            try {
                Class.forName(jdbcName);
                con=DriverManager.getConnection(url, user, password);
                //System.out.println("数据库连接成功");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //System.out.println("数据库连接失败");
                e.printStackTrace();
            }
            try {
                con = DriverManager.getConnection(url,user,password);
                System.out.println("连接成功");
    
    
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            return con;
        }
        public static void main(String[] args)throws SQLException { 
            Connection conn = getConnection();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from person";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            System.out.println(getConnection());
            while(rs.next()){
                System.out.println("成功");
            }
    
            }
    
           // return con;
            
        
        public static void close(Connection con) {
            if(con!=null)
                try {
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            
        }
        public static void close(Statement state, Connection conn) {
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close(ResultSet rs, Statement state, Connection conn) {
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    MOSS中实现自动上传图片
    2008年最后一天了
    MOSS中使用无刷新的日历日程控件破解版
    UC R2 Metro Tranning
    明智IT, 逆势成长概述
    RMS Client如何使用AD组策略部署
    MOSS & Project Server 2007
    MOSS & SSO 系列2
    Dynamics AX 2009 Trainning
    MOSS & SSO 系列4
  • 原文地址:https://www.cnblogs.com/j-y-s/p/14147461.html
Copyright © 2020-2023  润新知