zoukankan      html  css  js  c++  java
  • 期中考试前准备--数据库删除代码

    import java.sql.*;
    
    import java.util.Scanner;
    
    public class jdbcdelete {
    	public static void main(String[]args) {
    		final String URL = "jdbc:mysql://localhost:3306/test";
    		final String USERNAME = "root";
    	    final String PWD = "12345";
    	    Connection connection = null;
    	    Statement  stmt = null;
    		Scanner con=new Scanner(System.in);
    		String classname;
    		String teachername;
    		String didian;
    
    		classname=con.nextLine();
    		/*teachername=con.nextLine();
    		didian=con.nextLine();*/
    		try {
    			// a.导入驱动,加载具体的驱动类
    			Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
    			// b.与数据库建立连接
    			connection = DriverManager.getConnection(URL, USERNAME, PWD);
    			stmt = connection.createStatement();
    			
    			
    			//删除时如果是中文汉字要加单引号,如果是变量的话需要双引号单引号双引号
    			String sql = "delete from student where classname='"+classname+"'";
    			//String sql = "delete from student where classname='大学物理'";
    			// 执行SQL
    			int count = stmt.executeUpdate(sql); 
    			
    			
    			
    			// d.处理结果
    			if (count > 0) {  
    				System.out.println("操作成功!");
    			}
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		finally {
    			try {
    				 if(stmt!=null) stmt.close();// 对象.方法
    				 if(connection!=null)connection.close();
    			}catch(SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    
    }
    

      

  • 相关阅读:
    poj 2584 T-Shirt Gumbo (二分匹配)
    hdu 1757 A Simple Math Problem (乘法矩阵)
    矩阵之矩阵乘法(转载)
    poj 2239 Selecting Courses (二分匹配)
    hdu 3661 Assignments (贪心)
    hdu 1348 Wall (凸包)
    poj 2060 Taxi Cab Scheme (二分匹配)
    hdu 2202 最大三角形 (凸包)
    hdu 1577 WisKey的眼神 (数学几何)
    poj 1719 Shooting Contest (二分匹配)
  • 原文地址:https://www.cnblogs.com/jz-no-bug/p/14229683.html
Copyright © 2011-2022 走看看