Java jdbc访问sqlserver,oracle数据库 DEMO
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的更多相关文章
- Java jdbc访问sqlserver,oracle数据库
1.JDBC访问Oracle数据库 public class Jdbc_Oracle { // 静态代码块,只会执行一次,类似C#静态构造方法 static { try { // 加载数据库驱动一次 ...
- 使用JDBC访问SQLServer 2008
使用JDBC访问SQLServer 2008 // 准备数据库驱动程序 String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriv ...
- 设置sqlplus访问远程oracle数据库的最快方法
设置sqlplus访问远程oracle数据库的最快方法 时间:2010-01-21 10:57来源: 作者: 点击: 2次 设置sqlplus访问远程oracle数据库的最快方法,如果要连接远程数据库 ...
- java JDBC链接sqlserver/mysql/oracle
今天初学数据库的一些简单创建数据库和表,并进行简单的查询,插入. 接下学习的就是java工程中怎么链接数据库呢.主要的方法和用到的类如下. 切记,mysql需要的jar包 mysql-connecto ...
- Windows server2008 搭建ASP接口访问连接oracle数据库全过程记录--备用
真的是太不容易了,以前的时候在window server 2003上面搭建了一套asp+oracle的接口系统,就费了好大的劲儿,其实那会迷迷瞪瞪的也不知道怎么的就弄好了,也懒得管了.OK,从昨天到今 ...
- java连接VMware虚拟机Oracle数据库问题
最近在电脑上装了虚拟机,为的是在虚拟机上安装Oracle数据库,Oracle实在太占内存,配置低的电脑装个Oracle几乎就瘫了,没办法,搞个虚拟机玩玩.我虚拟机用的是xp系统,顺便怀念下经典.装好O ...
- java thin方式连接oracle数据库
本文主要描述通过thin方式连接oracle数据库 1.创建web project ,将D:\oracle\product\10.2.0\db_1\jdbc\lib(oracle安装目录)下的ojdb ...
- Java从入门到精通——数据库篇之JAVA中的对Oracle数据库操作
在Java中对Oracle数据库的操作分为两种:一.查询.二.非查询. 下面是我对其进行总结: 一.查询数据 /** * 根据用户代码查询 * @param userId * @return 如果存在 ...
- jdbc连接mysql/oracle数据库
driver-class-name : com.mysql.jdbc.Driver url : jdbc:mysql://localhost:3306/数据库名 username: root pa ...
随机推荐
- [django]模板template原理
django 中的render和render_to_response()和locals(): http://www.cnblogs.com/wangchaowei/p/6750512.html 什么是 ...
- C#集合中的Add与AddRange方法
C#.NET的集合主要位于System.Collections和System.Collections.Generic(泛型)这两个namespace中. 1.System.Collections 比如 ...
- liferay中数据库表的解析未完
页面布局 1:表layout 主要的字段有: 字段 privateLayout 0表示的是公开的页面 字段 layoutId 如果在同一个社区中有很多的界面,layoutId表示各个界面,按照顺序排列 ...
- php端口号设置和查看
- 025-du命令查看文件大小
1.查看某个目录下面所有文件占用空间大小并排序.du -sh 目录 2.排序.sort -h
- 017-linux正则表达式
一.单字符表示:1.特定字符:某个具体的字符. '1' 'a' '\.'2.范围内单个字符:单个字符[] [0-9] [259] [a-z] [abc] [A-Z] [ABC] [a-zA-Z] [, ...
- mysql的锁机制
一.读锁(共享锁/Share Locks,S锁). 1.select * from table_name where ... lock in share mode.(事务A) (1)这种方式是获取指定 ...
- LeetCode-MinimumDepthOfBinaryTree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- python实现常量const
新建const.py: #-*-coding:UTF-8-*- #Filename: const.py # 定义一个常量类实现常量的功能 # # 该类定义了一个方法__setattr()__,和一个异 ...
- Linux基础命令---sum,cksum
cksum 检查文件的crc是否正确,统计文件的字节数. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 cks ...