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 ...
随机推荐
- eclipse向svn提交代码的时候忽略部分资源配置
eclipse向svn提交代码的时候有 .settings, .project, .classpath, target等不需要上传,所以在eclipse中配置一下就不会显示了,方法如下图:
- kmeans聚类源代码
代码是在weka上二次开发的,但没有使用原来的kmeans代码,只是用了它的数据类Intances,先说下与它相关的几点东西. 一.KMeans算法简介 输入:聚类个数k,以及包含 n个数据对象的数据 ...
- [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- 摘要JSR168 PORLET标准手册汉化整理
本规范汉化资源搜集整理于网上并由我作了些修改和添加,主要为适应大陆的语辞.用语及其他未译之处. 由于本人于水平有限,如有错误,请各位高手指正:若有高见,希望不吝言辞,同为中国开源作项献. 特此严重感谢 ...
- Javascript-蔬菜运算价格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Typecho博客让文章列表页只显示摘要的方法
在当前主题的 index.php 文件中找到代码 <?php $this->content('阅读剩余部分...'); ?> 将其替换为 <?php $this->exc ...
- POJ 1182 并查集
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...
- OnClick,OnClientClick和OnServerClick的区别
OnClientClick是客户端事件处理方法,一般采用JavaScript来进行处理,也就是直接在IE端运行,一点击就运行 OnClick是服务器端事件处理方法,在服务器端也就是IIS中运行, ...
- iOS 绘图 (UIImage的一些操作)
UIGraphicsBeginImageContextWithOptions,本文主要在图片类型上下文中对图片进行操作,具体实现的功能: - 1.生成图片 - 2.绘制图片到视图 - 3.添加水印 ...