首页 技术 正文
技术 2022年11月6日
0 收藏 542 点赞 1,016 浏览 5581 个字

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();
}
}
} }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,120
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,592
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,437
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,208
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,844
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,929