day04 JDBC java数据库连接

day04 JDBC java数据库连接

yuxiaohu已关注

2017.12.03 16:12:19字数 139阅读 85

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();
        }
    }
}   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangchuang2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值