美文网首页Java程序设计基础
day04 JDBC java数据库连接

day04 JDBC java数据库连接

作者: yuxiaohu | 来源:发表于2017-12-03 16:12 被阅读0次

    1 导包

    mysql 提供的实现java接口的包路径
    C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.42-bin.jar

    Eclipse 导包 两种方式
    1:Project - Properties - Java Build Path - Add External JARs - 选择mysql包 // 指定包路径
    2:项目下建文件夹,复制包进去,鼠标右键 - Build Path - Add To Build Path // 拷贝包

    2 JDBC的步骤 (5步)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class Test {
        public static void main(String[] args) {
            try {
                //1、驱动类加载
                Class.forName("com.mysql.jdbc.Driver");
                //2、获取数据库连接
                Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/db01","root","123456");
                
                System.out.println(conn); //查看是否建立连接
                
                //3 、通过连接,创建一个语句对象,这个对象用于执行sql语句
                Statement stmt = conn.createStatement();
                
    //          String sql = "insert into employee value(6,1003,'武松',25,'男','景阳冈',200,2)"; //添加员工
    //          String sql = "delete from employee where num = 6 "; //删除员工
                String sql = "select * from employee"; //查询员工
                
                //4、执行sql语句
                //Statement 的三种常用方式
                
    //          int count =  stmt.executeUpdate(sql); //返回影响的行数 并执行sql更新语句
    //          System.out.println(count); 
    
    //          boolean flag = stmt.execute(sql);  //返回值为ResultSet(查询结果)才为true
    //          System.out.println(flag); 
                
                ResultSet rs = stmt.executeQuery(sql); //返回查询结果
                
                //使用迭代的方式遍历
                while(rs.next()){
                    System.out.println("姓名:" + rs.getString("name")
                    + "\t年龄:" + rs.getInt(4) + "\t地址:" + rs.getString(6));
                }
                
                //5、关闭连接
                stmt.close();
                conn.close();
                
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    

    3 封装JDBC工具包,以及使用外部配置文件

    DBUtils工具包,外部配置文件

    package com.yuxhu.jdbc.util;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    public class DBUtils {
        static String driver;
        static String url;
        static String user;
        static String password;
        //静态加载
        static{
            try {
                //加载外部属性文件
                Properties ppt = new Properties();
                //加载文件,加载的是类加器目录下面的db.properties
                ppt.load(DBUtils.class.getClassLoader().getResourceAsStream("db.properties"));
                
                driver = ppt.getProperty("jdbc.driver");
                url = ppt.getProperty("jdbc.url");
                user = ppt.getProperty("jdbc.user");
                password = ppt.getProperty("jdbc.password");
                
                //1、驱动类加载
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //获取连接
        public static Connection getConnection(){
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
        //关闭连接
        public static void closeConnection(Connection conn){
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    外部配置文件

    #这是一个数据库配置文件
    
    #数据库驱动
    jdbc.driver=com.mysql.jdbc.Driver
    #数据库数据源
    jdbc.url=jdbc:mysql://192.168.0.202:3306/mydb
    #数据库用户名
    jdbc.user=root
    #数据库密码
    jdbc.password=123456
    

    运行结果

    package com.yuxhu.jdbc.util;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class UtilTest {
        public static void main(String[] args) {
            try {
                //使用定义的工具类DBUtils获取连接
                Connection conn = DBUtils.getConnection();
                Statement stmt = conn.createStatement();
                String sql = "select * from employee"; // 查询员工
    
                // 4、执行sql语句
                ResultSet rs = stmt.executeQuery(sql); // 返回查询结果
                // 使用迭代的方式遍历
                while (rs.next()) {
                    System.out.println("姓名:" + rs.getString("name") + "\t年龄:" + rs.getInt(4) 
                    + "\t地址:" + rs.getString(6));
                }
    
                // 5、关闭连接
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }   
    

    相关文章

      网友评论

        本文标题:day04 JDBC java数据库连接

        本文链接:https://www.haomeiwen.com/subject/fmbvbxtx.html