前言
最近接触到了国产数据库达梦,因此想着写一个合集方便查阅连接驱动,以及配置连接。 创建用户、表以及表空间默认大家都会,因此就省略掉。 这里以Oracle为例子创建用户,表,赋予表空间。
1、达梦数据库(DM8) 2、Oracle11gR2 3、SqlServer2012 4、MySQL5.6
正文
主流数据库以及国产数据库驱动配置参考(端口默认没改动) 系统环境:Windows10 数据库: 1、达梦数据库(DM8 for Windows10)
2、Oracle11gR2 3、SqlServer2012 4、MySQL5.6
一、DM8数据库
注意:最终安装目录是你自己设置的,与我设置的有区别。
1、DM8数据库jar驱动
D:\software\dmdbms\drivers\jdbc
2、配置连接
jdbc.url=jdbc:dm:
二、MySQL数据库
1、mysql驱动
com.mysql.jdbc.Driver
2、配置连接
jdbc.url=jdbc:mysql://localhost(IP):3306(端口)/test?useUnicode=true&characterEncoding=utf-8
三、SQLserver数据库
1、sqlserver2012驱动
com.microsoft.sqlserver.jdbc.SQLServerDriver
2、配置连接
jdbc.url=jdbc:sqlserver://localhost(IP):1433(端口);instanceName=sqlserver2012;DatabaseName=test;integratedSecurity=true;
3、jar包驱动:
sqljdbc41.jar
四、Oracle数据库
1、oracle驱动
oracle.jdbc.driver.OracleDriver
2、jar驱动
ojdbc6.jar
3、配置连接
jdbc.url=jdbc:oracle:thin:@localhost(IP):1521(端口):orcl(实例名)
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
4.1、创建用户、表、生成数据
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
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();
}
}
}
持续更新优化中…
开心一刻
只要你找个好老板,找一份好工作,好好工作。 辛勤工作,早起晚归,经常加班。 早晚有一天,这个老板,会更有钱。换更好的车,换更好的房。 再给我一个机会,我想回到昨天,因为我安眠药吃多了。
开心一刻,乐呵乐呵。创作乐无边,学而思有境。你会发现,自己的知识宝库越来越丰富。好了,到此为止就是此篇文章的全部内容了,能看到这里的都是帅哥靓妹啊!!!善于总结,其乐不穷。好记性不如烂笔头,多收集自己第一次尝试的成果,收获也颇丰。白嫖有瘾,原创不易。
|