JDBC连接数据库

JDBC连接数据库

1、把jar包添加到库:DmJdbcDriver18.jar(在安装目录/drivers/jdbc下)因为我使用的jdk版本是11,所以用DmJdbcDriver18,它支持1.8及以上版本的jdk。

创建表并进行增删改查操作

package DM;
import java.sql.*;

/**
* @authorHyunfeng
* @date2021/1/4
*/
public class ConnectDM8 {
static String jdbcString = "dm.jdbc.driver.DmDriver";
static String urlString = "jdbc:dm://localhost:5236";// 定义连接用户名
static String userName = "SYSDBA";// 定义连接用户口令
static String password = "SYSDBA";// 定义连接对象
static Connection conn = null;
public static void findAll() {
try {
Class.forName(jdbcString);
conn = DriverManager.getConnection(urlString, userName, password);
Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery("select * from DMHR.JOB");
while(resultSet.next())
{
System.out.print("JOB_ID:"+resultSet.getInt(1));
System.out.print(" JOB_TITLE:"+resultSet.getString(2));
System.out.print(" MIN_SALARY:"+resultSet.getInt(3));
System.out.println(" MAX_SALARY:"+resultSet.getInt(4));
}
resultSet.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createTble()
{
try {
Class.forName(jdbcString);
conn = DriverManager.getConnection(urlString, userName, password);
Statement stmt = conn.createStatement();
// int i = stmt.executeUpdate("create table users(uid int,uname varchar(10));");
boolean flag = stmt.execute("create table users(uid int,uname varchar(10));");
if(flag)
System.out.println("建表成功");
else
System.out.println("建表失败");
System.out.println(flag);
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void insert()
{
try {
Class.forName(jdbcString);
conn = DriverManager.getConnection(urlString, userName, password);
Statement stmt = conn.createStatement();
int i = stmt.executeUpdate("insert into users values(1,'messi') ");
if (i!=0)
System.out.println("插入成功");
else
System.out.println("插入失败");
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delete()
{
try {
Class.forName(jdbcString);
conn = DriverManager.getConnection(urlString, userName, password);
Statement stmt = conn.createStatement();
int i = stmt.executeUpdate("delete users where uid=1");
if (i!=0)
System.out.println("删除成功");
else
System.out.println("删除失败");
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// createTble();
// insert();
// delete();
findAll();
}
}

 

JDBC连接数据库

 

ODBC配置

Linux 环境中配置ODBC(以root 配置)

1、检查gcc 包

[root@localhost ~]# rpm -aq|grep gcc

JDBC连接数据库

image-20210108153234880

没有的话就安装: yum install gcc -y

2、安装unixODBC-2.3.9

1)解压:tar -xvf unixODBC-2.3.9.tar.gz

cd unixODBC-2.3.0

2)./configure

3)make &&. make install

3、编辑配置文件odbc.ini、odbcinst.ini

vi /usr/local/etc/odbc.ini

[dm8]
Description = DM ODBC DSN
Driver = DM8 ODBC DRIVER
SERVER = localhost
UID = SYSDBA
PWD = SYSDBA
TCP_PORT = 5236

vi /usr/local/etc/odbcinst.ini

[DM8 ODBC DRIVER]
Description = ODBC DRIVER FOR DM8
Driver = /home/dmdba/dmdbms/bin/libdodbc.so

4、切换到dmdba用户登录

su - dmdba

isql dm8

JDBC连接数据库

windows

JDBC连接数据库

JDBC连接数据库

 

JDBC连接数据库

JDBC连接数据库

JDBC连接数据库

JDBC连接数据库

JDBC连接数据库

 

 

上一篇:JDBC基础&&各个接口和类


下一篇:JDBC连接程序