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. EM算法小结

    一.什么是EM算法? EM算法是机器学习中一个很重要的算法,即期望最大化算法,主要包括以下两个步骤: E步骤:estimate the expected values M步骤:re-estimate ...

  2. [LeetCode] 183. Customers Who Never Order_Easy tag: SQL

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...

  3. 修改class文件

    http://yucaifu1989.iteye.com/blog/1850500 http://blog.csdn.net/hexin373/article/details/6669813 使用ja ...

  4. JS在不同js文件中互相调用

    例如有这样一个html,里面有一个按钮,当按下时调用b.js文件中的方法b().而b()中又要调用a.js文件中的方法a().若要实现这个功能,必须注意,将要引入的Js文件代码放在</body& ...

  5. vs2010用NuGet(程序包管理)安装EF失败之解决办法

    今天用程序包管理控制台安装EF.报错.如下

  6. lambda函数和map函数

    lambda函数,简化了函数定义的书写形式,使代码更为简洁,但是使用自定义函数的定义方式更为直观,易理解 g = lambda x:x+1 #上面的lambda表达式相当于下面的自定义函数 def g ...

  7. HDU 4770

    这题说的是一在一个N*M的房间内,然后有些房间不能被灯光照亮,有一个灯可以转动方向,其他的灯只能在固定一个方向上,因为数据比较小,所以比较水,直接暴力的进行枚举就好了,但是还是 wa了很久,原因没认真 ...

  8. 用python实现websocket请求遇到的问题及解决方法。

    想要实现python的ws库功能,实时获取对方服务器ws协议返回的数据,查了下百度,用如下流程: ws = create_connection("wss://ws.xxxxxxx.info/ ...

  9. Java设计模式应用——模板方法模式

    所谓模板方法模式,就是在一组方法结构一致,只有部分逻辑不一样时,使用抽象类制作一个逻辑模板,具体是实现类仅仅实现特殊逻辑就行了.类似科举制度八股文,文章结构相同,仅仅具体语句有差异,我们只需要按照八股 ...

  10. pycharm中内看内建函数的定义

    鼠标方法在内建函数上,Ctrl+B,看内建函数的定义 如果想要看内置函数的具体实现细节,可以到python的lib目录下C:\Python27\Lib\,或者python的官网上 如果要看非内建的函数 ...