17°

JAVA--高级基础开发JDBC

JDBC实现CRUD操作
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//创建表
public class JDBCdemo1 {
    private static String url;
    private static String user;
    private static String password;
    static {
        url="jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&characterEnconding=utf8";
        user="root";
        password="105104";
    }
    public static void main(String[] args) {
        Connection  meth=null;
        Statement  state=null;
        try{
            //1 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2 获得数据库的连接对象 Connection
              meth=DriverManager.getConnection(url,user,password);
            if(meth!=null){
                System.out.println("连接成功");
            }else{
                System.out.println("连接失败");
            }
            // 3 获取Statement执行SQL语句的对象
             state=meth.createStatement();
            //4准备SQL语句
            String sql="create  table student(id int primary key auto_increment," +
                    "name  varchar(32) not null,gender varchar(32) not null,birthday date)";
            // 5 执行SQL(DDL没有返回结果)
            int  i=state.executeUpdate(sql);
            System.out.println("表创建成功");
        }catch(ClassNotFoundException  ce){
            ce.printStackTrace();
        }catch(SQLException  ce){
            ce.printStackTrace();
        }finally {
             try{
                 if(state!=null){
                     state.close();
                 }
             }catch (SQLException  ce){
                 ce.printStackTrace();
             }
            try{
                if(meth!=null){
                    meth.close();
                }
            }catch (SQLException  ce){
                ce.printStackTrace();
            }
        }
}

}

//添加表信息
public class JDBCdemo2 {
    private static String url;
    private static String user;
    private static String password;
    static {
        url="jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&characterEnconding=utf8";
        user="root";
        password="105104";
    }
    public static void main(String[] args){
        Connection meth=null;
        Statement state=null;
        try{
//            //1 加载驱动
//            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2 获得数据库的连接对象 Connection
            meth= DriverManager.getConnection(url,user,password);
            if(meth!=null){
                System.out.println("连接成功");
            }else{
                System.out.println("连接失败");
            }
            // 3 获取Statement执行SQL语句的对象
            state=meth.createStatement();
            //4准备SQL语句

            String  sql1="insert  into student(name,gender,birthday)values('李文杰','男','1996-01-05')";
            String  sql2="insert  into student(name,gender,birthday)values('陈天意','男','1997-01-05')";
            String  sql3="insert  into student(name,gender,birthday)values('郭朝旭','男','1993-01-05')";
            String  sql4="insert  into student(name,gender,birthday)values('桑凤娇','女','1998-01-05')";
            String  sql5="insert  into student(name,gender,birthday)values('星仔','男','1998-01-05')";

            //执行SQL语句
            state.executeUpdate(sql1);
            state.executeUpdate(sql2);
            state.executeUpdate(sql3);
            state.executeUpdate(sql4);
            state.executeUpdate(sql5);
        //释放资源
            state.close();
            meth.close();

        }catch (SQLException  ce){
            ce.printStackTrace();
        }
    }
}
//查询表中的信息
public class JDBCdemo3 {
    private static String url;
    private static String user;
    private static String password;
    static{
        url="jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&character=utf8";
        user="root";
        password="105104";
    }
    public static void main(String[] args){
        Connection  con=null;
        Statement   state=null;
       try{
           // 2 获得数据库的连接对象 Connection
         con=DriverManager.getConnection(url,user,password);
         //3 获的StateMent执行SQL语句的对象
           state=con.createStatement();

         //4 准备SQL 语句
         String  sql="select *  from  student ";
         //5执行SQL
           ResultSet  set=state.executeQuery(sql);
          System.out.println("查询成功");
           while (set.next()){
           //获取数据
               System.out.println("id"+set.getInt("id"));
               System.out.println("name"+set.getString("name"));
               System.out.println("gender"+set.getString("gender"));
               System.out.println("birthday"+set.getDate("birthday"));
           }

       }catch(SQLException  ce){
           ce.printStackTrace();
       }finally {
           try{
               if(state!=null){
                   state.close();
               }
           }catch (SQLException  ce){
               ce.printStackTrace();
           }
           try{
               if(con!=null){
                   con.close();
               }
           }catch (SQLException  ce){
               ce.printStackTrace();
           }
       }
    }
}
//修改表中的信息
public class JDBCdemo4 {
    private  static  String url;
    private  static  String user;
    private  static  String password;
    static {
        url="jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&character=utf8";
        user="root";
        password="105104";
    }
    public static void main(String[] args) {
        Connection  con=null;
        Statement  state=null;

        try{
            //1 获取数据库的连接对象
            con=DriverManager.getConnection(url,user,password);
            //2 获取Statement执行数据库语句的对象
            state=con.createStatement();
            // 3准备SQL 语句
            String sql="update student set name='李杰杰',gender='男生' where id=1";
            //4 执行SQL语句
            int i=state.executeUpdate(sql);
            if(i>0){
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        }catch (SQLException  ce){
            ce.printStackTrace();
        }finally {
            try{
                if(state!=null){
                    state.close();
                }
            }catch (SQLException  ce){
                ce.printStackTrace();
            }
            try{
                if(con!=null){
                    con.close();
                }
            }catch (SQLException  ce){
                ce.printStackTrace();
            }
        }

    }
}
//JDBC之删除
public class JDBCdemo5 {
    private  static  String url;
    private  static  String user;
    private  static  String password;
    static{
        url="jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&character=utf8";
        user="root";
        password="105104";
    }
    public static void main(String[] args) {
        Connection  con=null;
        Statement  state=null;
        try{
            // 1. 获取数据库的连接对象
            con= DriverManager.getConnection(url,user,password);
            // 2. 获取Statement 执行数据库的SQL语句
            state=con.createStatement();
            // 3. 准备SQL语句
            String sql="delete  from  student";
            // 4.执行SQL语句
            int i=state.executeUpdate(sql);
            if(i>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }catch (SQLException  ce){
            ce.printStackTrace();
        }finally {
            try{
                if(state!=null){
                    state.close();
                }
            }catch (SQLException  ce){
                ce.printStackTrace();
            }
            try{
                if(con!=null){
                    con.close();
                }
            }catch (SQLException  ce){
                ce.printStackTrace();
            }
        }

    }
}

本文由【李文杰-yaya】发布于开源中国,原文链接:https://my.oschina.net/u/4244602/blog/3136350

全部评论: 0

    我有话说: