java中连接Mysql的方法与连接SqlServer的方法有区别吗?

来源:百度知道 编辑:UC知道 时间:2024/07/02 19:31:22
java中连接Mysql的方法与连接SqlServer的方法有区别吗?
如果有,区别在哪里?

1、驱动程序不同

2、SqlServer可以用桥接(JDBC-ODBC)和直连(JDBC,但要打补丁对于2000来说);Mysql只能用直连。

String driverClassName = ...;
String url = ....;
String user = ....;
String pw = ....;

// java代码.
Class.forName(driverClassName);
Connection con = DriverManager.getConnection(url, user, pw);

连不同的数据库,java代码是一样的,只是前面列出的4个变量值不同。
通常情况都是通过配置文件指定那4个值,这样只要修改配置文件,就可以连接不同的数据库。

## MySQL

hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc\:mysql\://localhost\:3306/ra21?useUnicode\=true&characterEncoding\=gb2312
hibernate.connection.username=root
hibernate.connection.password=88888888

## MS SQL Server

#hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
#hibernate.connection.username sa
#hibernate.connection.password sa

## Microsoft Driver (not recommended!)