JDBC-API详解-Statement

 

复制代码
package Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import org.junit.Test;

public class JDBCdemo3_Statement {
    @Test
    public void testDML() throws Exception {
        //1.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url ="jdbc:mysql:///test?useSSL=false";
        String username="root";
        String password="1234";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.定义sql
        String sql="update test set money = 5000 where id=1";
        //4.获取执行sql的对象Statement
        Statement stmt = conn.createStatement();
        //5.执行sql
        int count = stmt.executeUpdate(sql);//返回受影响的行数
        //6.处理结果
        //System.out.println(count);
        if (count>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        //7.释放资源
        conn.close();
        stmt.close();
    }


    @Test
    public void testDDL() throws Exception {
        //1.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url ="jdbc:mysql:///test?useSSL=false";
        String username="root";
        String password="1234";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.定义sql
        String sql="create database test1";
        //4.获取执行sql的对象Statement
        Statement stmt = conn.createStatement();//执行DDL可能返回0
        //5.执行sql
        int count = stmt.executeUpdate(sql);//返回受影响的行数
        //6.处理结果
       System.out.println(count);
        if (count>0){
            System.out.println("执行成功");
        }else {
            System.out.println("执行失败");
        }
        //7.释放资源
        conn.close();
        stmt.close();
    }
}
复制代码

 

posted @   Karlshell  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示