1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 ImportJava.sql.ResultSet;5 Importjava.sql.SQLException;6 7 /**8 * Oracle Database-driven class encapsulation9 * @authorAdministratorTen * One */ A Public classOracle { - - FinalString driver= "Oracle.jdbc.driver.OracleDriver"; Oracle Driver
the FinalString url= "Jdbc:oracle:thin: @localhost: 1521:orcl"; 1521: Port ORCL: Database name - FinalString username= "News"; Oracle Database user name - FinalString password= "197520"; Oracle Database Password - + /* - * Package Connection + */ A PublicConnection getconnection () { atConnection cn=NULL;//claims-driven - - Try { - -Class.forName (Driver);//Load Driver - inCn=drivermanager.getconnection (Url,username,password);//Establish a connection - to}Catch(ClassNotFoundException e) { + //TODO auto-generated Catch block - e.printstacktrace (); the}Catch(SQLException e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng } - returncn; the } + A /* the * Close result set, operation, connection + */ - $ Public voidClose (ResultSet rs,preparedstatement ps,connection cn) { $ Try{ - //determine if the value is null - if(rs!=NULL){ the rs.close (); - }Wuyi if(ps!=NULL){ the ps.close (); - } Wu if(cn!=NULL){ - cn.close (); About } $}Catch(SQLException e) { - e.printstacktrace (); - } - } A +}
Java Connection to Oracle database