最近接触到了国产数据库达梦,因此想着写一个合集方便查阅连接驱动,以及配置连接。
- MySQL5.x & MySQL8.x
- 达梦数据库(DM8)
- Oracle11gR2
- SqlServer2012
主流数据库以及国产数据库驱动配置参考(端口默认没改动)
数据库:
- MySQL
- 达梦数据库(DM8 for Windows10)
- Oracle11gR2
- SqlServer2012
MySQL数据库
mysql驱动连接
旧版驱动:
新版驱动:
1
| com.mysql.cj.jdbc.Driver
|
配置连接
1 2
| jdbc.url=jdbc:mysql://localhost(IP):3306(端口)/test?useUnicode=true&characterEncoding=utf-8 jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
|
DM8数据库
注意:最终安装目录是你自己设置的,与我设置的有区别。
DM8数据库jar驱动所在路径:D:\dmdbms\drivers\jdbc
- jar包名称:DmJdbcDriver17.jar
- DM驱动:dm.jdbc.driver.DmDriver
配置连接
1 2
| jdbc.url=jdbc:dm://localhost(IP):5236(端口)/test(数据库名称) jdbc.url=jdbc:dm://localhost:5236/test
|
SQLserver数据库
sqlserver2012驱动
1
| com.microsoft.sqlserver.jdbc.SQLServerDriver
|
配置连接
1 2
| jdbc.url=jdbc:sqlserver://localhost(IP):1433(端口);instanceName=sqlserver2012;DatabaseName=test;integratedSecurity=true; jdbc.url=jdbc:sqlserver://localhost:1433;instanceName=sqlserver2012;DatabaseName=test;integratedSecurity=true;
|
jar包名称
Oracle数据库
准备工作
oracle驱动 & jar包
驱动:
1
| oracle.jdbc.driver.OracleDriver
|
jar包:ojdbc6.jar
配置连接:
1 2
| jdbc.url=jdbc:oracle:thin:@localhost(IP):1521(端口):orcl(实例名) jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
|
Oracle:创建用户、表、生成数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
create USER TEST;
grant resource,connect to TEST;
create table TEST.GIRL ( ID VARCHAR2(64), GIRL_NAME VARCHAR2(64), CUP_SIZE VARCHAR2(2), GIRL_AGE VARCHAR2(2), STU_NUM VARCHAR2(64) )
alter user test default tablespace USERS;
alter table TEST.GIRL add primary key(ID);
insert into TEST.GIRL values('1001','梦梦','C','16','TOLOVE16C');
select * from TEST.GIRL;
|
示例代码
使用编辑器:Spring Tool Suite4
测试连接:
- Oralce11g
- DM8
- MySQL8.x
- SqlServer2012
tips:注意事项写在代码注释中。
如下是demo示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
package com.example.demo.jdbc;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
public class TestConnJdbc { private static final Logger log = LoggerFactory.getLogger(TestConnJdbc.class);
static Connection conn = null; static PreparedStatement ps = null; static ResultSet rs = null;
public static void main(String[] args) throws Exception { TestSQLConnDM(); }
private static void TestSQLConnOralce() throws SQLException, ClassNotFoundException {
try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; String username = "TEST"; String password = "123456";
conn = DriverManager.getConnection(url, username, password); if(null != conn) { log.info("connect database success..."); System.out.println("连接成功!!!"); }else { log.error("connect database failed..."); System.out.println("连接失败!!!"); } String sql = "select * from girl"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { System.out.println("ID:" + rs.getString("ID")); System.out.println("姓名:" + rs.getString("girl_name")); System.out.println("年龄:" + rs.getString("girl_age")); System.out.println("尺寸:" + rs.getString("cup_size")); System.out.println("学号:" + rs.getString("stu_num")); } } finally { rs.close(); ps.close(); conn.close(); } }
private static void TestSQLConnDM() throws SQLException, ClassNotFoundException {
try { Class.forName("dm.jdbc.driver.DmDriver"); String url = "jdbc:dm://localhost:5236/test"; String username = "USERS"; String password = "123456789"; if(username!="USERS") { System.err.println("用户名错误或者不存在!!!"); }else if(password != "123456789" && password != null) { System.err.println("密码错误!!!"); }
conn = DriverManager.getConnection(url, username, password); if(null != conn) { log.info("connect database success..."); System.out.println("连接成功!!!"); }else { log.error("connect database failed..."); System.out.println("连接失败!!!"); } String sql = "select * from girl"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { System.out.println("ID:" + rs.getString("ID")); System.out.println("姓名:" + rs.getString("girl_name")); System.out.println("年龄:" + rs.getString("girl_age")); System.out.println("尺寸:" + rs.getString("cup_size")); System.out.println("学号:" + rs.getString("stu_num")); } } finally { rs.close(); ps.close(); conn.close(); } }
private static void TestSQLConnSqlServer() throws SQLException, ClassNotFoundException {
try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://localhost:1433;" + "database=girl;integratedSecurity=true;"; String username = "sa"; String password = "123456";
conn = DriverManager.getConnection(url, username, password); if(null != conn) { log.info("connect database success..."); System.out.println("连接成功!!!"); }else { log.error("connect database failed..."); System.out.println("连接失败!!!"); } String sql = "select * from girl"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { System.out.println("ID:" + rs.getString("ID")); System.out.println("姓名:" + rs.getString("girl_name")); System.out.println("年龄:" + rs.getString("girl_age")); System.out.println("尺寸:" + rs.getString("cup_size")); System.out.println("学号:" + rs.getString("stu_num")); } } finally { rs.close(); ps.close(); conn.close(); } }
private static void TestSQLConnMySQL() throws SQLException, ClassNotFoundException {
try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"; String username = "root"; String password = "123456"; if(username!="root") { System.err.println("用户名错误或者不存在!!!"); }else if(password != "123456" && password != null) { System.err.println("密码错误!!!"); }
conn = DriverManager.getConnection(url, username, password); if(null != conn) { log.info("connect database success..."); System.out.println("连接成功!!!"); }else { log.error("connect database failed..."); System.out.println("连接失败!!!"); } String sql = "select * from girl"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { System.out.println("ID:" + rs.getString("ID")); System.out.println("姓名:" + rs.getString("girl_name")); System.out.println("年龄:" + rs.getString("girl_age")); System.out.println("尺寸:" + rs.getString("cup_size")); System.out.println("学号:" + rs.getString("stu_num")); } } finally { rs.close(); ps.close(); conn.close(); } }
}
|
——END——