博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PreparedStatement
阅读量:6251 次
发布时间:2019-06-22

本文共 4599 字,大约阅读时间需要 15 分钟。

hot3.png

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Work21 {
// 加载驱动字符串
private static final String Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// 连接字符串
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=person";
// 用户名
private static final String USER = "sa";
// 密码
private static final String PASSWORD = "593850582";
/**
* 数据库连接
*/
private Connection con=null;
private PreparedStatement pst=null;
private ResultSet rs=null;
private Statement stmt=null;
Scanner scan = new Scanner(System.in);
// 加载驱动类
static {
try {
Class.forName(Driver);
} catch (Exception e) {
e.printStackTrace();
}
}
// 建立连接的方法
public Connection getConnection() {
try {
con = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
// 建立更新的方法
public int executeupdate() {
try {
pst = this
.getConnection()
.prepareStatement(
"update person set employeeSalary='4600' where employeeId = ?");
System.out.println("请输入要更新列表的employeeId:");
pst.setInt(1, scan.nextInt());
int line = pst.executeUpdate();
if (line > 0) {
System.out.println("更新数据成功");
}
return line;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭result
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 关闭Statement
if (pst != null) {
try {
pst.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 关闭Connection
if (con != null) {
try {
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
return 0;
}
// 建立删除的方法
public int executedelete() {
try {
pst = this.getConnection().prepareStatement(
"delete from person where employeeId=?");
System.out.println("请输入要删除列表的employeeId:");
pst.setInt(1, scan.nextInt());
int line = pst.executeUpdate();
if (line > 0) {
System.out.println("删除数据成功");
}
return line;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭result
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 关闭Statement
if (pst != null) {
try {
pst.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 关闭Connection
if (con != null) {
try {
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
return 0;
}
// 建立查询的方法
public int getQuery() {
//
try {
// 使用switch语句来判断语句执行的条件
switch (scan.nextInt()) {
case 1: {
stmt = con.createStatement();
rs = stmt.executeQuery("select * from person");
System.out.println("employeeId" + "   " + "employeeName"
+ "   " + "email" + "   " + "employeeSalary" + "   "
+ "departmentId");
while (rs.next()) {
System.out.println(rs.getInt(1) + "    " + rs.getString(2)
+ "    " + rs.getString(3) + "    "
+ rs.getString(4) + "   " + rs.getString(5));
}
System.out.println("查询全部已成功");
}
break;
case 2: {
pst = this.getConnection().prepareStatement(
"select * from person where employeeId=?");
System.out.println("please input your employeeId:");
pst.setInt(1, scan.nextInt());
rs = pst.executeQuery();
System.out.println("employeeId" + "   " + "employeeName"
+ "   " + "email" + "   " + "employeeSalary" + "   "
+ "departmentId");
while (rs.next()) {
System.out.println(rs.getInt(1) + "    " + rs.getString(2)
+ "    " + rs.getString(3) + "    "
+ rs.getString(4) + "   " + rs.getString(5));
}
System.out.println("依据employeeId查询成功");
}
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭result
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 关闭Statement
if (pst != null) {
try {
pst.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 关闭Connection
if (con != null) {
try {
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
return 0;
}
public int executeInsert() {
try {
// 获得Statement对象
pst = this
.getConnection()
.prepareStatement(
"insert into person values(4,'刘兴雨','125469873@qq.com','4000',3)");
int line = pst.executeUpdate();
if (line > 0) {
System.out.println("插入数据成功");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭result
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 关闭Statement
if (pst != null) {
try {
pst.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 关闭Connection
if (con != null) {
try {
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
return 0;
}
// 建立退出方法
public void exit() {
try {
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}

转载于:https://my.oschina.net/u/656507/blog/70539

你可能感兴趣的文章
linux内核数据结构之kfifo【转】
查看>>
c++学习笔记(新手学习笔记,如有错误请与作者联系)
查看>>
java集合复制和反转
查看>>
记录openlaw的反爬
查看>>
Matlab数据转化至python端,并写入数据库
查看>>
js 获取据当前时间n天前的时间
查看>>
json字符串与json对象的相互转换
查看>>
APM最佳实践:Web 2.0和AJAX四大优化战略
查看>>
Java优先队列一些问题
查看>>
【转】varchar,nvarchar长度、性能、及所占空间分析
查看>>
python控制语句执行流程(for)
查看>>
[NOIP2017 TG D2T2]宝藏(模拟退火)
查看>>
评价反馈
查看>>
Only the original thread that created a view hierarchy can touch its views的解决方案
查看>>
linux Git版本控制学习与Git服务器搭建
查看>>
配置Mqtt
查看>>
风暴数码论坛教程--初识Android
查看>>
【转】PHP乱码问题,UTF-8(乱码)
查看>>
熟悉常用的Linux操作
查看>>
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
查看>>