首页 > 代码库 > 数据库连接方式比较
数据库连接方式比较
Oracle连接方式
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConnection { public DBConnection(){ driver="oracle.jdbc.driver.OracleDriver"; url="jdbc:oracle:thin:@localhost:1521:myoracle"; dbusername="system"; dbpassword="admin"; this.getConection(); } private String driver; private String url; private String dbusername; private String dbpassword; private Connection con; private Statement sta; private ResultSet rs; /* * 得到Connection对象。 */ public Connection getConection(){ try { Class.forName(driver); con=DriverManager.getConnection(url,dbusername,dbpassword); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } return con; } /* * 得到Statement对象 */ public Statement getStatement(){ try { sta=con.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sta; } }
SQL SERVER
package com.wy.tools; import java.sql.*; /** * * @author Administrator */ public class JDBConnection { private final String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_register"; private final String userName = "sa"; private final String password = ""; private ResultSet rs = null; private Statement stmt = null; private Connection con = null; public JDBConnection() { try { Class.forName(dbDriver).newInstance(); } catch (Exception ex) { System.out.println("数据库加载失败"); } } private boolean creatConnection() { try { con = DriverManager.getConnection(url, userName, password); con.setAutoCommit(true); return true; } catch (SQLException e) { System.out.println(e.getMessage()); return false; } } public boolean executeUpdate(String sql) { if (null == con) { creatConnection(); } try { stmt = con.createStatement(); stmt.executeUpdate(sql); return true; } catch (SQLException e) { System.out.println(e.getMessage()); return false; } } public ResultSet executeQuery(String sql) { try { if (null == con) { creatConnection(); } stmt = con.createStatement(); try { rs = stmt.executeQuery(sql); return rs; } catch (SQLException e) { System.out.println(e.getMessage()); return null; } } catch (SQLException e) { System.out.println(e.getMessage()); return null; } } public void closeConnection() { if (null != rs) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != stmt) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != con) { try { con.close(); } catch (SQLException e) { } finally { con = null; } } } }
数据库连接方式比较
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。