jdbc连接数据库的7大步骤(以通过jdbc连接实现数据的插入、更新为例)

xiaoxiao2021-02-28  46

package cn.project_eg01; import org.junit.Test; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class Connect01 { private static String url = "jdbc:mysql://localhost:3306/myschool"; private static String user = "root"; private static String password = "214545"; @test public static void testcreate01() { Connection conn=null; Statement stmt=null; try { // 1.注册驱动连接 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn=(Connection) DriverManager.getConnection(url, user, password); // 3.创建statement stmt=(Statement) conn.createStatement(); // 4.准备sql语句 String sql = "INSERT INTO cat(id,name,health,love,strain) VALUES('002','小花',100,90,'雪纳瑞')"; // 5.执行sql语句 int count = stmt.executeUpdate(sql); // 6.输出 System.out.println("输出了" + count + "行"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new RuntimeException(e); }finally{ // 7.关闭连接 if(stmt!=null) try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } // *********更新 public static void testupdate03() { Connection conn=null; Statement stmt=null; // String setname="小白"; // int id=002; try { // 1.注册驱动连接 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn=(Connection) DriverManager.getConnection(url, user, password); // 3.创建statement stmt=(Statement) conn.createStatement(); // 4.准备sql语句 // String sql = "UPDATE cat SET name='"+setname+"' WHERE id="+id+""; String sql ="UPDATE cat SET name='小白' WHERE id=002"; // 5.执行sql语句 int count = stmt.executeUpdate(sql); // 6.输出 System.out.println("输出了" + count + "行"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new RuntimeException(e); }finally{ // 7.关闭连接 if(stmt!=null) try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } // ********主函数***** public static void main(String[] args) { // TODO Auto-generated method stub //System.out.println("连接成功!"); testcreate01(); testinsert02(); testupdate03(); } public static void testinsert02() { Connection conn=null; Statement stmt=null; try { // 1.注册驱动连接 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn=(Connection) DriverManager.getConnection(url, user, password); // 3.创建statement stmt=(Statement) conn.createStatement(); // 4.准备sql语句 String sql = "INSERT INTO cat(id,name,health,love,strain) VALUES('003','小花',100,90,'雪纳瑞')"; // 5.执行sql语句 int count = stmt.executeUpdate(sql); // 6.输出 System.out.println("输出了" + count + "行"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new RuntimeException(e); }finally{ // 7.关闭连接 if(stmt!=null) try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } }  

转载请注明原文地址: https://www.6miu.com/read-83185.html

最新回复(0)