Java语言基础知识之习题四
最近复习到了JDBC,挑一个具有代表性的题目放上来吧(第四题)import java.sql.*;
import java.util.Scanner;
public class Four {
public static void main(String[] args) throws ClassNotFoundException {
String addres = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mysql";
String user = "root";
String password = "sa";
String sqlStr = "select name,id,author,publisher from book";
try {
Class.forName(addres);
System.out.println("加载驱动成功");
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功");
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
System.out.println("创建statement 成功");
//查询数据
ResultSet rs = st.executeQuery(sqlStr);
System.out.println("-------------------------------");
System.out.println("查询成功");
printRs(rs);
System.out.println("-------------------------------");
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要查找的书名");
String search = sc.nextLine();
boolean f = false;
System.out.println("当前查到的信息如下");
while (rs.next()) {
if (rs.getString(("name")).equals(search)) {
f = true;
System.out.print(rs.getString(("id")) + " ");
System.out.print(rs.getString(("name")) + " ");
System.out.print(rs.getString(("author")) + " ");
System.out.println(rs.getString(("publisher")) + " ");
}
}
if (f) {
System.out.println("请选择你要删除书的编号");
int del = sc.nextInt();
String d = "delete from book where id = " + del;
int row = st.executeUpdate(d);
if (row != 0) {
//row如果为0,即删除成功
System.out.println("删除成功");
} else {
//row为1,输出失败
System.out.println("删除失败");
}
}
rs.close();
st.close();
con.close();
} catch (SQLException e) {
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("Reason: " + e.getMessage());
}
}
private static void printRs(ResultSet rs) throws SQLException {
while (rs.next()) {
System.out.print(rs.getString(("id")) + " ");
System.out.print(rs.getString(("name")) + " ");
System.out.print(rs.getString(("author")) + " ");
System.out.println(rs.getString(("publisher")) + " ");
}
//再次把指针放到最前面
rs.beforeFirst();
}
}
在新标签打开所有链接复制所有链接URL复制所有链接URL(反向)复制所有链接标题 + URL复制所有链接标题 + URL (MD)复制所有链接标题 + URL (BBS)复制所有链接标题 + URL (筛选)复制所有链接标题 + URL (设置复制格式)在新标签页打开所有图片链接在一个标签页显示所有图片链接
复选框 - 选中
复选框 - 取消
复选框 - 反选
单选框 - 选中
单选框 - 取消
特殊单选框 - 选中 楼主666666 好好学习这些基础,以后用其他框架的时候比这个简单多了,难的会了简单的就更容易了 lidelongqi 发表于 2021-4-5 17:05
好好学习这些基础,以后用其他框架的时候比这个简单多了,难的会了简单的就更容易了
哈哈 谨遵教诲,地基确实是很重要的 学习了,支持。
页:
[1]