JDBC教程学习1

持久化:把数据保存到可掉电式存储设备中以供之后使用。
JDBC访问数据库的形式:
1.直接使用API去访问数据库。
2.间接使用JDBC的API去访问数据库。
JDBC:是一种用于执行SQL语句的Java API,可以为多种数据库提供统一访问,它由一组用Java语言编写的类与接口组成。

public class COnnectionTest{
//获取数据库连接对象
@Test
public void test throws Exception{
     //加载注册驱动
     Class.forName ("com.mysql.jdbc.Driver");
     //获取连接对象
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","admin");
   }
}

操作JDBC的步骤:
口诀:贾琏欲执事。
1)贾:加载注册驱动
2)琏:获取连接对象
3)欲:创建语句对象
4)执:执行SQL语句
5)事:释放资源C
Connection接口:数据库的连接对象
Statement createStatement();创建静态语句对象。
Statement接口:用于执行静态SQL的 语句对象,用于把SQL语句发送到数据库中去执行,并返回执行之后的结果。
对于DDL:返回查询的结果集。
对于DML:返回受影响的行数。
执行DDL语句创建表,处理异常:

public class DDlTest{
     //创建t_student表
     @Test
     public void test1() throws Exception{
         String sql ="CREATE TABLE t_student (id bigint(20) PRIMARY KEY AUTO_INCREMENT,name varchar(20),age int(11));"; 
        Class.forName ("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","admin");
        Statement st=con.createStatement();
        int row=st.executeUpdate(sql);
        st.close();
        con.close();
        System.out.println(row);
        }
        //声明需要关闭的资源
        Connection con=null;
        Statement st=null;
        try{
              Class.forName ("com.mysql.jdbc.Driver");
              con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","admin");
              st=con.createStatement();
              st.executeUpdate(sql);
         }catch (Exception e){
         }finally{
          //释放资源
              try{
                   if(st!=null){
                      st.close();
                      }
            }catch (Exception e){
         }finally{
         try{
               if(con!=null){
                      con.close();
                      }
 
         }catch (Exception e){
         }
       }
   }
}

执行DML操作(增删改):
执行DML操作的代码和DDL相同,唯一不同的是执行SQL语句。

public class DMLTest{
//增加
public void testInsert() throws Exception{
      String sql="INSERT INTO t_student (name,age) VALUES ('李杰',30)";
       Class.forName ("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","admin");
        Statement st=con.createStatement();
        int row=st.executeUpdate(sql);
        st.close();
        con.close();
        System.out.println(row);
     
}
//修改
public void testUpdate() throws Exception{
         String sql="UPDATE  t_student SET name='Mike',age=28 WHERE id=1";
        Class.forName ("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","admin");
        Statement st=con.createStatement();
        int row=st.executeUpdate(sql);
        st.close();
        con.close();
        System.out.println(row);
     
}
//删除
public void testDelete() throws Exception{
         String sql="DELETE FROM t_student WHERE id=1 ";
        Class.forName ("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo","root","admin");
        Statement st=con.createStatement();
        int row=st.executeUpdate(sql);
        st.close();
        con.close();
        System.out.println(row);
     
}
}

查询操作:
ResultSet executeQuery(String sql):执行DQL语句,返回的是结果集对象
ResultSet接口:执行DQL操作后,会产生一个结果集,而ResultSet就是对结果集做的封装。
boolean next():先判断光标是否能往下移动,如果可以就移动。


在Java中,只有在JDBC和IPA中序号是从1开始的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值