JDBC连接数据库,严谨版本代码

 
package com.zzk.cn;
import java.sql.*;

public class TestJdbc {
    public static void main(String[] args) {
    	ResultSet rs=null;
    	Statement stmt=null;
    	Connection conn=null;
    	try{
    	System.out.println("HelloWorld!");
    	Class.forName("oracle.jdbc.driver.OracleDriver");
    	//new oracle.jdbc.driver.OracleDriver;
    	conn=DriverManager.getConnection("","","");
    	stmt=conn.createStatement();
    	rs=stmt.executeQuery("select * from dept");

    	while(rs.next()){
    		rs.getString("deptno");
    		rs.getInt("id");
    		
    	}
    	}catch(ClassNotFoundException e ){
    		e.printStackTrace();
    	}catch(SQLException e ) {
    		e.printStackTrace();
    	}finally {
    	try {
    		if(rs!=null){
			rs.close();
			rs=null;
    		}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
    	try {
    		if(stmt!=null) {
			stmt.close();
			stmt=null;
    		}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
    	try {
    		if(conn!=null) {
			conn.close();
			conn=null;
    		}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
    	}
    	
    	
    }
}


 

 

 

package com.zzk.cn;
import java.sql.*;

public class TestDml {
    public static void main(String[] args) {
    	Statement stmt=null;
    	Connection conn=null;
    	
    	try {
    		Class.forName("oracle.jdbc.driver.OracleDrivaer");
    		conn=DriverManager.getConnection("");
    		stmt=conn.createStatement();
    		String sql="insert into dep values(98,'game','id')";
    		stmt.executeUpdate(sql);
    	}catch (ClassNotFoundException e) {
    		e.printStackTrace();
    	}catch(SQLException e) {
    		e.printStackTrace();
    	}finally {
    		try{
    			if(stmt!=null) {
    				stmt.close();
    				stmt=null;
    			}
    			if(conn!=null) {
    				conn.close();
    				conn=null;
    			}
    		}catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }
}


 

上一篇:庖丁解牛-系统基本指标对应分析工具图


下一篇:jpa @onetomany 级联查询时会有重复数据,去重问题