• Java数据库——连接关闭、增删改查


    连接数据库

    //=================================================
    // File Name       :	MySQL_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    
    //主类
    //Function        : 	MySQL_demo
    public class MySQL_demo {
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Connection conn = null;					//数据库连接
    		try{
    			Class.forName(DBDRIVER);		//加载MYSQL JDBC驱动程序
    		}catch(ClassNotFoundException e){
    			e.printStackTrace();
    		}
    		try{
    			//连接MySQL数据库时,要写上连接的用户名和密码
    			conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    		System.out.println(conn);
    		try{
    			conn.close(); 				//数据库关闭
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    	}
    }
    

     

    建立一个user表

    CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(30) NOT NULL,password VARCHAR(32) NOT NULL,age INT NOT NULL,sex VARCHAR(2),birthday DATE);
    

    <1>数据库的更新操作

    执行数据库插入操作,执行一次插入一条

    //=================================================
    // File Name       :	MySQL_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    
    //主类
    //Function        : 	MySQL_demo
    public class MySQL_demo {
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		
    		Connection conn = null;					//数据库连接
    		Statement stmt = null;					//数据库操作
    		String sql = "INSERT INTO user(name,password,age,sex,birthday)"
    				+"VALUES('张三','mima',30,'男','2014-01-11')";
    		Class.forName(DBDRIVER);			//加载驱动程序
    		//连接MySQL数据库时,要写上连接的用户名和密码
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		stmt = conn.createStatement();		//实例化Statement对象
    		stmt.executeUpdate(sql);				//执行数据库更新操作
    		stmt.close(); 										//操作关闭
    		conn.close(); 										//数据库关闭
    	}
    }
    
    //=================================================
    // File Name       :	MySQL_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    
    //主类
    //Function        : 	MySQL_demo
    public class MySQL_demo {
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		
    		Connection conn = null;					//数据库连接
    		Statement stmt = null;					//数据库操作		
    		String name = "李四";
    		String password = "pwd";
    		int age = 22;
    		String sex = "女";
    		String birthday = "2012-01-01";
    		String sql = "INSERT INTO user(name,password,age,sex,birthday)"
    				+"VALUES('"+name+"','"+password+"',"+age+",'"+sex+"','"+birthday+"')";
    		Class.forName(DBDRIVER);			//加载驱动程序
    		//连接MySQL数据库时,要写上连接的用户名和密码
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		stmt = conn.createStatement();		//实例化Statement对象
    		stmt.executeUpdate(sql);				//执行数据库更新操作
    		stmt.close(); 										//操作关闭
    		conn.close(); 										//数据库关闭
    	}
    }
    

     

    <2>执行数据库的修改

    //=================================================
    // File Name       :	MySQL_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    
    //主类
    //Function        : 	MySQL_demo
    public class MySQL_demo {
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		
    		Connection conn = null;					//数据库连接
    		Statement stmt = null;					//数据库操作
    		
    		int id = 2;
    		String name = "王五";
    		String password = "pwd2";
    		int age = 25;
    		String sex = "女";
    		String birthday = "2002-11-21";
    		String sql = "UPDATE user SET name= '"+name+"' , password='"+password+"' , age="+age+" , sex='"+sex+"' , birthday='"+birthday+"' WHERE id="+id;
    		Class.forName(DBDRIVER);			//加载驱动程序
    		//连接MySQL数据库时,要写上连接的用户名和密码
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		stmt = conn.createStatement();		//实例化Statement对象
    		stmt.executeUpdate(sql);				//执行数据库更新操作
    		stmt.close(); 										//操作关闭
    		conn.close(); 										//数据库关闭
    	}
    }
    

    <3>执行数据库删除操作

    //=================================================
    // File Name       :	MySQL_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    
    //主类
    //Function        : 	MySQL_demo
    public class MySQL_demo {
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		
    		Connection conn = null;					//数据库连接
    		Statement stmt = null;					//数据库操作
    		
    		int id = 3;
    		String sql = "DELETE FROM user WHERE id=" + id;
    		Class.forName(DBDRIVER);			//加载驱动程序
    		//连接MySQL数据库时,要写上连接的用户名和密码
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		stmt = conn.createStatement();		//实例化Statement对象
    		stmt.executeUpdate(sql);				//执行数据库更新操作
    		stmt.close(); 										//操作关闭
    		conn.close(); 										//数据库关闭
    		
    	}
    }
    

    在JDK1.7中,java.sql中的Connection,Statement,ResultSet接口都继承了AutoCloseable,所以使用了try-with-resource

     

  • 相关阅读:
    python 列表
    pytho set集合
    python 字典
    并发编程(原理篇 上)
    面向对象分析方法
    python 小记 整数与小数id
    python入门 第二天笔记
    python 元组问题解决
    python入门第一天作业。讲师写的代码。
    python学习笔记enumerate()与range(len)运用及赋值小计
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5302112.html
Copyright © 2020-2023  润新知