import java.sql.*;
public class GetDBData {
public static void main(String[] args) {
// TODO Auto-generated method stub
Statement stmt = null;
Connection conn = null;
int lowNumber = 0;
try {
//1、加载JDBC的本地驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.创建数据库连接(本地ExpressTest库)
//2.1、定义数据库位置和库名称
String DBurl = "jdbc:sqlserver://localhost:1433; DatabaseName=ExpressTest";
//2.2 定义访问数据库所需的用户名和密码
String user = "sa";
String password = "sa@123";
//2.3 获取数据库连接
conn = DriverManager.getConnection(DBurl, user, password);
System.out.println("获取连接成功!");
//3.准备数据库指令(以静态查询语句为例)
//3.1 准备静态SQL语句字符串变量
String sql = "select * from D_ShipList_Main";
//3.2 创建静态SQL语句对象
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//4. 在数据库连接上执行准备好的SQL语句
ResultSet rs = stmt.executeQuery(sql);
System.out.println("查询语句执行成功!");
//5. 利用循环操作结果集
//5.1 获取总行数方法1:通过循环累加计数
int rows = 0;
while(rs.next()) {
System.out.println(rs.getString("PKID") + "\t; "
+ rs.getString("SendGoodsCustomerNO") + "\t; "
+ rs.getString("SendGoodsCustomer") + "\t; "
+ rs.getString("SendGoodsDate") + "\t; "
+ rs.getString("SendGoodsAddr"));
rows++;
}
System.out.println("查到 " + rows + "行数据。");
//5.2 获取总行数方法2:通过移动游标
// rs.last();
// int rowCount = rs.getRow(); // 获取当前行号,即总行数
// rs.beforeFirst(); // 将游标重置到初始位置
// System.out.println("Total rows: " + rowCount);
//5.3 获取总行数方法3:通过count(*),此方法性能最优
// Statement stmtCount = conn.createStatement();
// ResultSet rs1 = stmt.executeQuery("SELECT COUNT(*) AS totalCount FROM D_ShipList_Main");
// int rowCount1 = 0;
// if (rs1.next()) {
// rowCount1 = rs1.getInt("totalCount");
// }
// System.out.println("Total rows: " + rowCount1);
//
//******使用PreparedStatement对象向数据库中新增数据行
//新增客户函数addCustomer_1() 和 addCustomer_2()
// System.out.println("\n*********测试在T_Customer数据表中新增记录********");
// String code = "CA001134";
// String name = "张大强";
// String phone= "13901234567";
// String fax = "0531-87654322";
// String address = "山东济南二环东路7300号";
// String postCode = "250000";
// int csType = 2;
// //利用动态SQL语句对象向T_Customer表中新增一行数据
//// int rv = addCustomer_1(conn, code, name, phone, fax, address, postCode, csType);
// //利用储存过程向T_Customer表中新增一行数据
// int rv = addCustomer_2(conn, code, name, phone, fax, address, postCode, csType);
// if(rv==0) {
// System.out.println("新增客户成功!");
// }
// else {
// System.out.println("新增客户失败!");
// }
String cusCode = "CA001134";
int rv1 = delCustomer(conn, cusCode);
if(rv1 == 0) {
System.out.println("成功:已从T_Customer表中删除客户代码为" + cusCode + "的客户!");
}
else {
System.out.println("失败:未从T_Customer表中删除客户代码为" + cusCode + "的客户!");
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int addCustomer_1(Connection conn,
String code,
String name,
String phone,
String fax,
String address,
String postCode,
int csType) {
String strSql = "insert into T_Customer(CustomerCode, CustomerName, Phone, CustomerAddress, CustomerType) "
+ "values(?,?,?,?,?)";
try (PreparedStatement pStmt = conn.prepareStatement(strSql)) {
pStmt.setString(1, code);
pStmt.setString(2, name);
pStmt.setString(3, phone);
pStmt.setString(4, address);
pStmt.setInt(5, csType);
pStmt.executeUpdate();
// System.out.println("为T_Customer表增加记录成功!");
return 0;
}
catch(SQLException e) {
e.printStackTrace();
return -1;
}
}
//利用储存过程向T_Customer表中新增一行数据
public static int addCustomer_2(Connection conn,
String code,
String name,
String phone,
String fax,
String address,
String postCode,
int csType) {
try {
CallableStatement csmt = conn.prepareCall("{call USP_addNewCustomer(?,?,?,?,?,?,?)}");
csmt.setString(1, code);
csmt.setString(2, name);
csmt.setString(3, phone);
csmt.setString(4, fax);
csmt.setString(5, address);
csmt.setString(6, postCode);
csmt.setInt(7, csType);
csmt.executeUpdate();
return 0;
}
catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
//从T_Customer表中删除一个客户
public static int delCustomer(Connection conn, String code) {
String strSql = "delete from T_Customer where CustomerCode like ?";
try (PreparedStatement pStmt = conn.prepareStatement(strSql)) {
pStmt.setString(1, code);
pStmt.executeUpdate();
return 0;
}
catch(SQLException e) {
e.printStackTrace();
return -1;
}
}
}
在以上代码中完成以下任务:
第一,增加一个方法,在方法中利用PreparedStatement对象,将T_Customer中CustomerCode内容为“Cus00009”的数据行CustomerName字段内容修改为“大众日报社济南分社”。
第二,增加一个方法,结果目标与第1个任务一致,但要求利用CallableStatement对象实现;
第三,实现一个方法,查找D_ShipList_Main表中价格最高的运单,列出运单号、发件人姓名、发件日期、收件地址。
最新发布