jdbc复习

今天又maven写了一下jdbc复习。

结果发现,怎么都运行不成功。
就在我一遍又一遍的找错之后,发现,竟然没导包,没添加依赖!!!!

共勉,一定要细心。

可能是因为,间隔时间长了,所以一开始也没注意,然后,一直不知道哪里错了。

现在上干货。

首先打开数据库,创建数据库db4.
在数据库中创建student表。
id,name,age,birthday。
分别为int,varchar,int,Date类型。

前戏做完之后。

进入正题。

首先,创建一个db.properties
在里面写入连接数据库的信息,url,driver,username,password。

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db4?serverTimezone=GMT%2B8
username=root
password=root

之后,创建一个Util包,在util包里创建一个daoUtil类。

package com.bai.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class daoUtil {
    private static String driver;
    private static String url;
    private static String password;
    private static String username;
    static {
        Properties properties = new Properties();
        InputStream in = daoUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(in);

            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
          
        } catch (Exception e) {
            e.printStackTrace();
        }
        

    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}

之后就可以,使用这个工具类了


package com.bai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class selectDao {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = baseDao.getConnection();
            String sql = "insert into student (id,name,age,date) values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,1);
            statement.setString(2,"laobai");
            statement.setInt(3,20);
            statement.setDate(4, new java.sql.Date(new Date().getTime()));
            int i = statement.executeUpdate();
            if (i>0){
                System.out.println("cha ru cheng gong");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }



    }
}

大概就这样。
注意,preparestatement,里的占位符,是从第一位开始的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吃西瓜的鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值