中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java JDBC 小例子

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
1. 使用MySQL建立一個test數(shù)據(jù)庫,里面建立一個mytable表,3列(id,name,age);將url, user, password配制成properties文件,放到 工程resource源代碼包下面,這里將其命名為db_connect.properties文件
    url = jdbc:mysql://localhost:3306/test  
    user = root  
    password = admin  

2. 將數(shù)據(jù)庫連接封裝到一個類中,利用配置文件連接,靜態(tài)返回connection
    package study.jdbc;  
      
    import java.io.FileInputStream;  
    import java.io.FileNotFoundException;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.sql.Connection;  
    import java.sql.DriverManager;  
    import java.sql.SQLException;  
    import java.util.Properties;  
      
    public class DBConnect {  
        static String url;  
        static String user;  
        static String password;  
      
        /** 
         * 獲取一個JDBC連接,返回一個Connection對象 
         * @return connection 
         */  
        public static Connection connectDB() {  
            Connection connection = null;  
            readProperties();  
            try {  
                Class.forName("com.mysql.jdbc.Driver");  
                connection = DriverManager.getConnection(url, user, password);  
            } catch (SQLException e) {  
                e.printStackTrace();  
            } catch (ClassNotFoundException e) {  
                e.printStackTrace();  
            }  
            return connection;  
        }  
      
        /** 
         * 讀取properties文件,獲取url,user,password 
         */  
        private static void readProperties() {  
            String fileName = "resouce/db_connect.properties"; //相對于工程  
            Properties properties = new Properties();  
            try {  
                InputStream in = new FileInputStream(fileName);  
                properties.load(in);  
                in.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            url = properties.getProperty("url");  
            user = properties.getProperty("user");  
            password = properties.getProperty("password");  
        }  
      
    }  

3.  對數(shù)據(jù)庫進行增刪改查的測試,主要練習使用PreparedStatement
    package study.jdbc;  
      
    import java.sql.Connection;  
    import java.sql.PreparedStatement;  
    import java.sql.ResultSet;  
    import java.sql.SQLException;  
      
    public class TestMain {  
        public static void main(String[] args) {  
            Connection connection = DBConnect.connectDB(); //獲取數(shù)據(jù)庫連接  
            TestMain test = new TestMain();  
            try { //測試  
                test.clear(connection);   
                test.insert(connection); //增  
                test.query(connection);  
                System.out.println("----------");  
                test.delete(connection); //刪    
                test.query(connection);    
                System.out.println("----------");  
                test.update(connection); //改  
                test.query(connection);  //查  
                connection.close(); //關(guān)閉數(shù)據(jù)庫連接  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
          
        /** 
         * 使用PreparedStatement,效率高 
         * 動態(tài)執(zhí)行SQL(帶參數(shù)的SQL語句),是Statement子接口 
         * 對數(shù)據(jù)庫進行insert,用帶參數(shù)的語句批量插入 
         * @param connection 
         * @throws SQLException 
         */  
        public void insert(Connection connection) throws SQLException {  
            String sql = "INSERT INTO mytable(id,name,age) values (?,?,22);";  
            PreparedStatement pr = connection.prepareStatement(sql);  
            for (int i = 1; i <= 3; i++) {  
                pr.setInt(1, i);  
                pr.setString(2, "demo"+i);  
                pr.executeUpdate();  
            }  
        }  
      
        /** 
         * 對數(shù)據(jù)庫進行delete 
         * @param connection 
         * @throws SQLException 
         */  
        public void delete(Connection connection) throws SQLException {  
            String sql = "delete from mytable where id=2;";  
            PreparedStatement pr = connection.prepareStatement(sql);  
            pr.executeUpdate();  
        }  
      
        /** 
         * 對數(shù)據(jù)庫進行update 
         * @param connection 
         * @throws SQLException 
         */  
        public void update(Connection connection) throws SQLException {  
            String sql = "UPDATE mytable SET name='new' WHERE id=1;";  
            PreparedStatement pr = connection.prepareStatement(sql); // 創(chuàng)建statement對象發(fā)送SQL到數(shù)據(jù)庫  
            pr.executeUpdate(); // 執(zhí)行UPDATE  
      
        }  
      
        /** 
         * 查詢query遍歷結(jié)果集 
         * @param connection 
         * @throws SQLException 
         */  
        public void query(Connection connection) throws SQLException {  
            String sql = "select * from mytable;";  
            PreparedStatement st = connection.prepareStatement(sql);  
            ResultSet re = st.executeQuery(); // 查詢,返回單個ResultSet對象  
            while (re.next()) {  
                int id = re.getInt(1);  
                String name = re.getString(2);  
                int age = re.getInt(3);  
                System.out.println(id + "\t" + name + "\t" + age);  
            }// 遍歷結(jié)果集  
        }  
          
        /** 
         * 清空表 
         * @param connection 
         * @throws SQLException 
         */  
        public void clear(Connection connection) throws SQLException {  
            String sql = "delete from mytable;";  
            PreparedStatement pr = connection.prepareStatement(sql);  
            pr.executeUpdate();  
        }  
    }  

標簽: Mysql 代碼 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Android系統(tǒng)下檢測Wifi連接互聯(lián)網(wǎng)是否正常的代碼

下一篇:Java日期時間操作代碼示例大全