码迷,mamicode.com
首页 > 数据库 > 详细

JDBC开发模式

时间:2014-10-03 22:30:15      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   for   文件   

一】代码模块———Demo.java 

       public class Demo {
                private static Connection connection;
                private static Statement statement;
                private static ResultSet rs;
            
                public static void main(String[] args) {
            
                    connection = JDBCUtils.getConnection();
            
                    try {
                        statement = connection.createStatement();
                        rs = statement.executeQuery(SqlMapping.QEURY_ALL);
            
                        while (rs.next()) {
                            System.out.println(rs.getInt("id") + " : "
                                    + rs.getString("name") + " : " + rs.getString("gender")
                                    + " : " + rs.getString("salary"));
                        }
            
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
            
                    JDBCUtils.closeStream(rs, statement, connection);
            
                }

            

 


            
 二】工具模块———JdbcUtil.java   

                    public class JDBCUtils {
        
                    private static Properties properties;
                    //得到数据库驱动
                    static {
                        InputStream inputStream  = JDBCUtils.class.getClassLoader().getResourceAsStream("com/suse/jdbc/db.properties");
                        properties = new Properties();
                        try {
                            properties.load(inputStream);
                            Class.forName(properties.getProperty("driver"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    
                    //得到数据库连接
                    public static Connection getConnection(){
                            Connection connection  = null;
                            try {
                                connection = DriverManager.getConnection(properties.getProperty("url"), properties);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                            return connection;
                    }
                    
                    
                    //关闭流
                    public static void closeStream( ResultSet rs, Statement stmt, Connection conn) {
                        
                        if (null != rs) {
                            try {
                                rs.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        if (null != stmt) {
                            try {
                                stmt.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        if (null != conn) {
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }    
                }
            

 

三】配置文件模块———db.properties

        driver = com.mysql.jdbc.Driver
            url  = jdbc:mysql://127.0.0.1:3306/mydb
            user = root
            password = root

 

JDBC开发模式

标签:style   blog   color   io   os   ar   java   for   文件   

原文地址:http://www.cnblogs.com/SkyGood/p/4005337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!