zoukankan      html  css  js  c++  java
  • JDBC编程6步

    /*
    	JDBC编程六步:(背)
    		1)注册驱动(告诉java程序,即将要连接的是哪个品牌的数据库)
    		2)获取连接(表示JVM的进程和数据库进程之间的通道打开了,这数据进程间的通信,重量级的,使用完要关闭)
    		3)获取数据库操作对象(专门执行sql语句的对象)
    		4)执行sql语句(DQL,DML)
    		5)处理查询结果集(只有第四步执行的是select语句时,才有第五步处理查询结果集)
    		6)释放资源(使用完资源后一定要关闭资源,java和数据库属于进程间通信,开启后一定要关闭)
    */
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.getConnection;
    
    public clas JDBCTest01{
    	public static void main(String []args){
    	Statement stmt=null;
    	Connection conn=null;
    	try{
    		//1.注册驱动
    		Driver driver=new com.mysql.jdbc.Driver();	//多态,父类型引用指向子类型对象
    		//Driver driver = new oracle.jdbc.driver.OracleDriver();	//oracle的驱动
    		DriverManager.registerDriver(driver);
    		
    		//2.获取连接
    		String url="jdbc:mysql://127.0.0.1:3306/bjpowernode";
    		/
    			jdbc:mysql://	协议
    			127.0.0.1		ip地址
    			3306			mysql数据库端口号
    			bjpowernode		具体的数据库实例名
    			说明:
    				localhost和127.0.0.1都是本机ip地址
    			什么是通信协议?有什么用?	
    				通信协议是通信之前就定好的数据传送格式
    				数据包具体怎么传数据,格式提前定好的
    			oracle的URL:
    				jdbc:oracle:thin:@localhost:1521:orcl
    				
    		*/
    		String user="root";
    		String password="123456";
    		conn=DriverManager.getConnection(url,user,password);
    		System.out.println("数据库连接对象="+conn);
    		
    		//3.获取数据库操作对象(Statement专门执行sql语句)
    		stmt=conn.createStatement();
    		
    		//4.执行sql
    		String sql="insert into dept(deptno,dname,loc) valuses(50,'人事部','北京')"; //jdbc中的sql语句不需要提供分号结尾
    		int count=stmt.executeUpdate(sql);	//返回值是:影响数据库中的记录条数
    		System.out.println(count==1?"保存成功":"保存失败");
    		
    		//5.处理查询结果集
    		
    	}
    	catch(SQLException e){
    		e.printStackTrace();
    	}finally{
    	//6.释放资源
    		//为了保证资源一定释放,在finally语句块中关闭资源,并且要遵循从小到大依次关闭
    		//分别对其try,catch
    		try{
    			if(stmt!=null){
    				stmt.close();
    			}
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    		try{
    			if(conn!=null){
    			conn.close();
    			}
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    	}	
    	}
    }

    模板:
      
    import  java.sql.*;
    
    public class JDBCTest02{	
    	public static void main(String [] args){
    		Connection conn=null;
    		Statement stmt=null;
    		try{
    		//1.注册驱动
    		DriveraManager.registerDriver(new com.mysql.jdbc.Driver());
    		//2.获取链接
    		conn=DriverManager.getConnection("jdbc:mysql://localhost:d3306/bjpowernode","root","123456");
    		//3.获取数据库操作对象
    		stmt=conn.createStatement();
    		//4.执行sql语句
    		String sql="delete from dept where deptno=40";
    		int count=stmt.executeUpdate(sql);
    		System.out.println(count==1?"删除成功":"删除失败");
    	}catch(SQLException e){
    		e.printStackTrace();
    	}finally{
    		//6.释放资源
    		if(smt!=null){
    			try{
    				smt.close();
    			}catch(SQLException e){
    				e.printStackTrace();
    			}
    		}
    		if(conn!=null){
    			try{
    				conn.close();
    			}catch(SQLException e){
    				e.printStackTrace();
    			}
    		}
    	}
    		
    		
    	}
    }

      

  • 相关阅读:
    关于 Unity WebGL 的探索(二)
    关于 Unity WebGL 的探索(一)
    Ghostscript 中 ps2pdf 命令在 windows msys 下的运行错误问题。
    编译 Windows 版本的 Unity Mono(2017-03-12 20:59)
    Windows 下使用 mingw+msys 交叉编译 Android Unity Mono
    关于 UGUI 字体花屏或乱码。
    从 NavMesh 网格寻路回归到 Grid 网格寻路。
    Unity光照图UV显示
    2DPlatformer-SLua 编辑器 UI 美化
    SnapDragon Profiler 学习
  • 原文地址:https://www.cnblogs.com/-slz-2/p/15449443.html
Copyright © 2011-2022 走看看