JDBC编程

上一篇《JDBC编程入门》对理论知识进行了详细的说明,此处则对代码进行演示。

package com.wrh.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import com.mysql.jdbc.StreamingNotifiable;
/**
 * <p>
 * 
 * </p> 
 * @Copyright (C),
 * @author hero_wrh
 * @Date:2018年8月6日
 * JDBC
 */
public class JDBC01 {
    Connection con;
    Statement stmt;

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/student";
        String username="root";
        String pwd="123456789";
        //获取连接
        Connection con =  DriverManager.getConnection(url, username, pwd);
        return con; 
    }
    //插入
    @Test
    public void insert() throws ClassNotFoundException, SQLException{
        try {
            con = getConnection();
            stmt = con.createStatement();
            String sql = "insert into test values('lisi', '23')";
            stmt.execute(sql);
            System.out.println("插入成功");
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            stmt.close();
            con.close();
        }   
    }
    //修改
    @Test
    public void update() throws ClassNotFoundException, SQLException {
        try {
            con = getConnection();
            stmt = con.createStatement();
            String sql="update test set age='18' where age='23'";
            stmt.executeUpdate(sql);
            System.out.println("修改成功");
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            stmt.close();
            con.close();
        }   
    }
    //删除
    @Test
    public void delete() throws SQLException, ClassNotFoundException {
        try {
            con = getConnection();
            stmt = con.createStatement();
            String sql = "delete from test where name='lisi'";
            stmt.executeUpdate(sql);
            System.out.println("删除成功!");
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            stmt.close();
            con.close();
        }
    }
    //查询
    @Test
    public void query() throws SQLException, ClassNotFoundException {
        try {
            con = getConnection();
            stmt = con.createStatement();
            String sql = "select * from test";
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                String name = rs.getString(1);
                String age = rs.getString(2);
                System.out.println(name + ",   " + age);
            }
            System.out.println("查询成功");
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            stmt.close();
            con.close();
        }
    }





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值