2021/8/14

 8:30-9:00 学习打卡
9:30-12:00 看视频
15:00-17:00 写qq项目

jdbc的应用

1.注册驱动

Class.forName("com.mysql.jdbc.Driver");

2.获取连接对象

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test");

3.获取执行sql语句对象

Statement st=con.createStatement();

4.定义sql

String s="INSERT INTO sutent ( id, NAME ) VALUES ( 1, 'jjj' );";

5.执行sql语句

st.executeUpdate(s1);

6.释放资源

con.close(); 

st.close(); 

package qqserver.mode;
import java.sql.*;
//mysql的连接

public class Mysql {
    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/test";
    private String user=null;
    private String pass=null;
    private String sql="mysql.ini";

    public Mysql(String user,String pass) {
        this.user=user;
        this.pass=pass;
    }

    public static void main(String[] args) {
        Mysql mq=new Mysql("root","123456");
        Connection con=null;
        Statement st=null;
        try {
            Class.forName(driver);//注册驱动
            con=DriverManager.getConnection(url);//获取连接
            st=con.createStatement();
            String s1="INSERT INTO sutent ( id, NAME ) VALUES ( 1, 'jjj' );"; //插入id为1 名字为jjj进入表sutent
            String s2="SELECT id FROM sutent;"; //查看表sutent所有的id元素
            int i1=st.executeUpdate(s1);
            System.out.println("修改了"+i1+"行");
            ResultSet i2=st.executeQuery(s2);
            while(i2.next()){
                i2.getInt(1);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(con!=null)
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if(st!=null)
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值