永发信息网

JAVA 请在原代码的基础上完善!加入显示全部记录、修改指定记录、删除指定记录及追加新的记录功能!

答案:2  悬赏:50  手机版
解决时间 2022-01-01 15:47
  • 提问者网友:轻浮
  • 2021-12-31 19:24

import java.sql.*;
public class JDBC {
public void getStudentById(String id) { //根据学号查询学生信息
String driverClass = "com.mysql.jdbc.Driver";
String connectUrl ="jdbc:MySQL://localhost:3306/welcomestudent";
String userName = "root"; //用户名
String userPass = "00000000"; //密码
Connection con =null;
Statement stmt =null;
try {
Class.forName(driverClass); //加载、注册JDBC驱动程序
} catch (ClassNotFoundException ce) {
ce.printStackTrace();
}
try {
con = DriverManager. getConnection(connectUrl, userName,userPass); //建立数据库连接
stmt = con .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); //建立Statement对象
ResultSet rs = null;
String sqlStr = "select * from student where id="+id;
rs = stmt.executeQuery(sqlStr); //执行SQL语句,返回结果集
if (rs.next()) { //处理结果
System.out.print(rs.getString(1)+",");
System.out.print(rs.getString(2)+",");
System.out.print(rs.getInt(3)+",");
System.out.println(rs.getString(4));
}
rs.close(); //释放资源
stmt.close();
} catch (SQLException e) { //捕获和处理异常
e.printStackTrace();
}finally {
try{
if (con!=null || !con.isClosed()){
con.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[]args){
JDBC jdbc=new JDBC(); //创建类的实例
jdbc.getStudentById(("10203321")); //调用方法查询并显示结果
}
}
最佳答案
  • 五星知识达人网友:杯酒困英雄
  • 2021-12-31 20:11
可以给你写·
全部回答
  • 1楼网友:煞尾
  • 2021-12-31 21:09
package com.file; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.objectinputstream; import java.io.objectoutputstream; public class test { static { book book = new book(); book.setbookid("
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯