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(); } }
标签:JDBC,String,System,API,Statement,sql,conn,out From: https://www.cnblogs.com/Karl-hut/p/17481393.html