JDBC连接SQL数据库的基本操作

先写一个加载JDBC驱动和创建数据库连接和关闭数据库连接

package second;
import java.sql.*;
public class link {
    static {//加载JDBC
        try{
            String driverName="com.mysql.cj.jdbc.Driver";
            Class.forName(driverName);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){//创建数据库连接
        Connection con=null;
        try {
            con=DriverManager.getConnection("jdbc:mysql://localhost/tudou","root","");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  con;
    }
    public static void close(ResultSet rs,Connection con,Statement statement,PreparedStatement pstmt){//关闭数据库连接
        try {
            if(rs !=null)rs.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            if(statement !=null) statement.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            if(con !=null) con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            if(pstmt !=null) pstmt.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

再写一个用java实现关于sql中的增删改查的功能

package second;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.text.ParseException;

public class demof{
    Connection con;//数据库连接
    ResultSet rs;//保存查询结果
    Statement statement;//数据库操作
    PreparedStatement pstmt;

    public Statement getStatement() {
        return statement;
    }

    public ResultSet getRs() {
        return rs;
    }

    public Connection getCon() {
        return con;
    }

    public PreparedStatement getPstmt() {
        return pstmt;
    }

    public demof(Connection con){
        this.con=con;
        try {
            statement=con.createStatement();//实例化Statement对象
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public void createTable() throws  SQLException{//创建表
        try{
            String sql="create table if not exists test1(id int,age int,name varchar(100))";
            statement.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public void  Insert()throws SQLException{//插入
        String sql1="insert into test1 values(1,19,'土豆啊土豆')";
        String sql2 ="insert into test1 values(2,20,'土豆呀土豆')";
        String sql3="insert into test1 values(3,21,'土豆和土豆')";
        String sql4="insert into test1 values(?,?,?)";//PreparedStatement操作
        pstmt=con.prepareStatement(sql4);
        pstmt.setInt(1,4);
        pstmt.setInt(2,25);
        pstmt.setString(3,"你好呀");
        statement.addBatch(sql1);
        statement.addBatch(sql2);
        statement.addBatch(sql3);
        int[] results=statement.executeBatch();
        pstmt.executeUpdate();
    }
    public void Select() throws SQLException{//查询
        String sql="select id,age,name from test1";//查询不能使用*在实际开发中必须明确写出查询列
        rs=statement.executeQuery(sql);
        while(rs.next()){
            String id=rs.getString("id");
            int age=rs.getInt("age");
            String name=rs.getString("name");
            System.out.println(id+"\t"+age+"\t"+name);
        }

    }
    public void Delete() throws  SQLException{//删除
        String sql="delete from test1 where id=2";
        statement.executeUpdate(sql);
    }
    public static void main(String[] args) {
        Connection con=link.getConnection();
        demof start=new demof(con);
        try {
            start.createTable();
//            start.Insert();
//            start.Select();
            //start.Delete();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
           link.close(start.getRs(),start.getCon(),start.getStatement(),start.getPstmt());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值