java程序中sql server和oracle、mysql的连接字符串有什么不同?

2024-12-21 11:17:37
推荐回答(2个)
回答1:

瘦连接,写法基本上一致, URL的写法上略有区别。

MySQL:
MySQL.Driver =com.mysql.jdbc.Driver
MySQL.URL =jdbc:mysql://localhost:3306/myDB?autoReconnect=true
MySQL.User =xxxx
MySQL.Password =xxxx

Oracle:
oracle.driver =oracle.jdbc.driver.OracleDriver
oracle.url =jdbc:oracle:thin:@localhost:1521:OraTest
oracle.user =xxxx
oracle.password= xxxx

SQL Server:
.mssql.Driver =net.sourceforge.jtds.jdbc.Driver
.mssql.URL =jdbc:jtds:sqlserver://localhost:1433/myDB
.mssql.User =xxxx
.mssql.Password = xxxx

回答2:

链接数据库用到JDBC,一般链接数据库都要用到数据库驱动、链接字符串、登录名、登录密码。这4个属性可以提取到一个父类中,每一种数据库写一个类继承父类。要是这些都没问题了,就可以用楼上哥们儿说的,简单工厂模式,简单工厂是用到了java中的多态特性。 import java.sql.Connection; public abstract class BaseConnect { public BaseConnect(String driverClass, String url, String loginName, String password) { super(); this.driverClass = driverClass; this.url = url; this.loginName = loginName; this.password = password; } protected String driverClass; protected String url; protected String loginName; protected String password; public abstract void initDriver(); public abstract Connection getConnect(); public abstract void closeConnect(Connection conn); } import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class OraclConnect extends BaseConnect { public OraclConnect(String driverClass, String url, String loginName, String password) { super(driverClass, url, loginName, password); // TODO Auto-generated constructor stub } @Override public void initDriver() { try { Class.forName(driverClass); } catch (ClassNotFoundException e) { e.printStackTrace(); } } @Override public Connection getConnect() { try { return DriverManager.getConnection(url, loginName, password); } catch (SQLException e) { e.printStackTrace(); } return null; } @Override public void closeConnect(Connection conn) { try { if (conn != null && !conn.isClosed()) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } 以此类推,上面代码是我随便一写,你自己再改进下。 蓝屏