1.去官网下载对应版本的Ojdbc.jar(oracle)、sqljdbc.jar(sqlserver)。
2.放置到项目lib文件夹下
3.项目右键->Build Path->configure build path->libraries->add external jars。
4.连接方式:
Oracle数据库:
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";//SQL数据库引擎 public static final String connectDB = "jdbc:oracle:thin:@localhost:1521:DBName";//连接字符串 1521为Oracle数据库默认端口 public static final String username = "sa";//用户名 public static final String password = "123456";//密码 public static void main(String[] args) throws IOException { Connection con = null; try { Class.forName(DBDRIVER); con = DriverManager.getConnection(connectDB, username, password); if (con != null) { System.out.println("连接成功!"); } } catch (Exception e) { System.out.println(e.getMessage()); } }
SQLSERVER数据库:
public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎 public static final String connectDB = "jdbc:sqlserver://localhost:1433;databaseName=DBName";//连接字符串 1433为sqlserver默认端口,可以不设置 public static final String username = "sa";//用户名 public static final String password = "123456";//密码 public static void main(String[] args) throws IOException { Connection con = null; try { Class.forName(DBDRIVER); con = DriverManager.getConnection(connectDB, username, password); if (con != null) { System.out.println("连接成功!"); } } catch (Exception e) { System.out.println(e.getMessage()); } }