1.JDBC访问Oracle数据库

 public class Jdbc_Oracle {

     // 静态代码块,只会执行一次,类似C#静态构造方法
static {
try {
// 加载数据库驱动一次
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} //main函数,数据的操作
public static void main(String[] args) {
del();
//exec();
select();
} // 添加、增删改
public static void exec() {
Connection con = null;
PreparedStatement cmd = null;
try {
// 在控制台输入
Scanner scanner = new Scanner(System.in);
System.out.print("请输入类型名称:");
String name = scanner.nextLine(); // 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
// 创建sql命令对象
cmd = con.prepareStatement("insert into ProductType(Name,Up) values(?,?)");
// 设置参数
cmd.setString(1, name);
cmd.setInt(2, 0);
// 执行sql返回影响行数
int result = cmd.executeUpdate();
System.out.println("影响行数:" + result);
} catch (Exception e) {
// 把错误的堆栈信息显示在控制台
e.printStackTrace();
} finally {
try {
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // 删除、增删改
public static void del() {
Connection con = null;
PreparedStatement cmd = null;
try {
// 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
// 创建sql命令对象
cmd = con.prepareStatement("delete from ProductType where Id=?");
// 设置参数
cmd.setInt(1, 21);
// 执行sql返回影响行数
int result = cmd.executeUpdate();
System.out.println("影响行数:" + result);
} catch (Exception e) {
// 把错误的堆栈信息显示在控制台
e.printStackTrace();
} finally {
try {
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // 查询
public static void select() {
Connection con = null;
PreparedStatement cmd = null;
ResultSet result = null;
try {
// 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "databasename", "datapwd");
// 创建sql命令对象
cmd = con.prepareStatement(
"select id, name, up from producttype where Id>? and Name like ?");
// 设置参数
cmd.setInt(1, 5);
cmd.setString(2, "%能%");
// 执行sql获得结果集
result = cmd.executeQuery();
// 取出结果集中的数据
while (result.next()) {
System.out.print(result.getInt("Id") + "\t");
System.out.print(result.getInt(1) + "\t");
System.out.print(result.getString("Name") + "\t");
System.out.print(result.getInt("Up") + "\t\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
result.close();
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} // 删除、增删改
public static void del() {
Connection con = null;
PreparedStatement cmd = null;
try {
// 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "gmall", "orcl");
// 创建sql命令对象
cmd = con.prepareStatement("delete from ProductType where Id=?");
// 设置参数
cmd.setInt(1, 21);
// 执行sql返回影响行数
int result = cmd.executeUpdate();
System.out.println("影响行数:" + result);
} catch (Exception e) {
// 把错误的堆栈信息显示在控制台
e.printStackTrace();
} finally {
try {
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // 查询
public static void select() {
Connection con = null;
PreparedStatement cmd = null;
ResultSet result = null;
try {
// 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "gmall", "orcl");
// 创建sql命令对象
cmd = con.prepareStatement(
"select id, name, up from producttype where Id>? and Name like ?");
// 设置参数
cmd.setInt(1, 5);
cmd.setString(2, "%能%");
// 执行sql获得结果集
result = cmd.executeQuery();
// 取出结果集中的数据
while (result.next()) {
System.out.print(result.getInt("Id") + "\t");
System.out.print(result.getInt(1) + "\t");
System.out.print(result.getString("Name") + "\t");
System.out.print(result.getInt("Up") + "\t\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
result.close();
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

2.JDBC访问sqlserver数据库

 public class Jdbc_SQLServer {

 // 静态代码块,只会执行一次,类似C#静态构造方法
static {
try {
// 加载驱动一次
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
del();
//exec();
select();
} // 添加、增删改
public static void exec() {
Connection con = null;
PreparedStatement cmd = null;
try {
// 在控制台输入
Scanner scanner = new Scanner(System.in);
System.out.print("请输入类型名称:");
String name = scanner.nextLine(); // 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
// 创建sql命令对象
cmd = con.prepareStatement("insert into [ProductType]([Name],Up) values(?,?)");
// 设置参数
cmd.setString(1, name);
cmd.setInt(2, 0);
// 执行sql返回影响行数
int result = cmd.executeUpdate();
System.out.println("影响行数:" + result);
} catch (Exception e) {
// 把错误的堆栈信息显示在控制台
e.printStackTrace();
} finally {
try {
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // 删除、增删改
public static void del() {
Connection con = null;
PreparedStatement cmd = null;
try {
// 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
// 创建sql命令对象
cmd = con.prepareStatement("delete from [ProductType] where Id=?");
// 设置参数
cmd.setInt(1, 12);
// 执行sql返回影响行数
int result = cmd.executeUpdate();
System.out.println("影响行数:" + result);
} catch (Exception e) {
// 把错误的堆栈信息显示在控制台
e.printStackTrace();
} finally {
try {
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // 查询
public static void select() {
Connection con = null;
PreparedStatement cmd = null;
ResultSet result = null;
try {
// 建立数据库连接,指定数据库用户名,密码,数据库名称
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=GoMall", "sa", "sa");
// 创建sql命令对象
cmd = con.prepareStatement(
"SELECT [Id],[Name],[Up] FROM [GoMall].[dbo].[ProductType] where Id>? and Name like ?");
// 设置参数
cmd.setInt(1, 5);
cmd.setString(2, "%能%");
// 执行sql获得结果集
result = cmd.executeQuery();
// 取出结果集中的数据
while (result.next()) {
System.out.print(result.getInt("Id") + "\t");
System.out.print(result.getInt(1) + "\t");
System.out.print(result.getString("Name") + "\t");
System.out.print(result.getInt("Up") + "\t\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
result.close();
cmd.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }

Java jdbc访问sqlserver,oracle数据库 DEMO的更多相关文章

  1. Java jdbc访问sqlserver,oracle数据库

    1.JDBC访问Oracle数据库 public class Jdbc_Oracle { // 静态代码块,只会执行一次,类似C#静态构造方法 static { try { // 加载数据库驱动一次 ...

  2. 使用JDBC访问SQLServer 2008

    使用JDBC访问SQLServer 2008 // 准备数据库驱动程序 String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriv ...

  3. 设置sqlplus访问远程oracle数据库的最快方法

    设置sqlplus访问远程oracle数据库的最快方法 时间:2010-01-21 10:57来源: 作者: 点击: 2次 设置sqlplus访问远程oracle数据库的最快方法,如果要连接远程数据库 ...

  4. java JDBC链接sqlserver/mysql/oracle

    今天初学数据库的一些简单创建数据库和表,并进行简单的查询,插入. 接下学习的就是java工程中怎么链接数据库呢.主要的方法和用到的类如下. 切记,mysql需要的jar包 mysql-connecto ...

  5. Windows server2008 搭建ASP接口访问连接oracle数据库全过程记录--备用

    真的是太不容易了,以前的时候在window server 2003上面搭建了一套asp+oracle的接口系统,就费了好大的劲儿,其实那会迷迷瞪瞪的也不知道怎么的就弄好了,也懒得管了.OK,从昨天到今 ...

  6. java连接VMware虚拟机Oracle数据库问题

    最近在电脑上装了虚拟机,为的是在虚拟机上安装Oracle数据库,Oracle实在太占内存,配置低的电脑装个Oracle几乎就瘫了,没办法,搞个虚拟机玩玩.我虚拟机用的是xp系统,顺便怀念下经典.装好O ...

  7. java thin方式连接oracle数据库

    本文主要描述通过thin方式连接oracle数据库 1.创建web project ,将D:\oracle\product\10.2.0\db_1\jdbc\lib(oracle安装目录)下的ojdb ...

  8. Java从入门到精通——数据库篇之JAVA中的对Oracle数据库操作

    在Java中对Oracle数据库的操作分为两种:一.查询.二.非查询. 下面是我对其进行总结: 一.查询数据 /** * 根据用户代码查询 * @param userId * @return 如果存在 ...

  9. jdbc连接mysql/oracle数据库

    driver-class-name : com.mysql.jdbc.Driver url : jdbc:mysql://localhost:3306/数据库名 username:   root pa ...

随机推荐

  1. python排序函数sort()与sorted()区别

    sort是容器的函数:sort(cmp=None, key=None, reverse=False) sorted是python的内建函数:sorted(iterable, cmp=None, key ...

  2. http协议基础(二)请求和响应报文的构成

    http协议用于客户端和服务器之间的通信,请求访问资源的一方称为客户端,而提供资源响应的一方称为服务器端. 下面就是客户端和服务端之间简单的通信过程 PS:请求必须从客户端建立通信,服务端没收到请求之 ...

  3. Bus,Exclusive access,memory attribute

    指令LDREX,STREX是在armv6中新加的指令,配合AMBA3--AXI中的lock[1:0]信号. 在Atomic Access一节中是这么规定的:ARLOCK[1:0]/AWLOCK[1:0 ...

  4. Shell篇(三)TC Shell

    Shell脚本的首行一般写为"#!+路径"来告诉系统,以路径所指定的程序来解释此脚本. 可以写为 #! /bin/tcsh -f (-f表示快速启动,不启动~/.tcshrc) S ...

  5. pandas练习(二)------ 数据过滤与排序

    数据过滤与排序------探索2012欧洲杯数据 相关数据见(github) 步骤1 - 导入pandas库 import pandas as pd 步骤2 - 数据集 path2 = ". ...

  6. vim的简单配置

    本文大部分内容转载自:https://blog.csdn.net/lhy2932226314/article/details/69668891 vim是从 vi 发展出来的一个文本编辑器.功能丰富,在 ...

  7. Java代码质量度量工具大阅兵

    FindBugs FindBugs, a program which uses static analysis to look for bugs in Java code. It is free so ...

  8. Linux服务器配置---ssh配置

    Ssh配置     通过配置文件,我们可以有效的管理ssh 1.空闲时间关闭连接 1)修改配置文件“/etc/ssh/sshd_config”,设置clientAliveInterval和client ...

  9. WordPress存在DoS拒绝服务漏洞,推荐删除根目录下的xmlrpc.php

    WordPress存在DoS拒绝服务漏洞,推荐删除根目录下的xmlrpc.php受影响的版本:WordPress 3.9.x-3.9.1WordPress 3.8.x-3.8.3WordPress 3 ...

  10. SQL学习之Can't connect to MySQL server on localhost (10061)

    最近升级 了系统,开机后连接MySQL报错,Can't connect to MySQL server on localhost (10061): 估计是升级系统清除了以前的缓存设置,网上很多方法是命 ...