package conn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Conn { /** * 连接数据库 * * @author Administrator * */ public Connection getConnection() { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1522:xe", "smn", "orcl"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /** * 添加数据 * @param conn * @param sql * @return 返回成功添加记录数 */ public int updateMethord(Connection conn,String sql){ int count=0; Statement st; try { st = conn.createStatement(); count=st.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return count; } public ResultSet selectMethord(Connection conn,String sql){ ResultSet rs=null; Statement st; try { st = conn.createStatement(); rs=st.executeQuery(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public void closeMethord(Connection conn){ if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
时间: 2024-11-08 01:30:01