代码拉取完成,页面将自动刷新
package company.com;
import java.sql.*;
public class Text_Student {
public static void main(String[] args) {
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection=
DriverManager.getConnection("jdbc:mysql://localhost:3306/stu_information?user=root&password=sn20000904&useUnicode=true&characterEncoding=UTF-8");
//statement创建简单的命令对象statement
statement=connection.createStatement();
String sql="select student.id,student.sn,student.name,classes.name,classes.coures from classes\n" +
"join student on classes.id=student.classes_id\n" +
"where classes.name='计算机系2019级1班';";
resultSet=statement.executeQuery(sql);
while (resultSet.next()){
int id=resultSet.getInt("student.id");
String sn=resultSet.getString("student.sn");
String student_name=resultSet.getString("student.name");
String classes_name=resultSet.getString("classes.name");
String coures=resultSet.getString("classes.coures");
Student student=new Student(id,sn,student_name,classes_name,coures);
System.out.println(student.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(connection!=null)
connection.close();
if(statement!=null)
statement.close();
if(resultSet!=null)
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
private static class Student{
private int id;
private String sn;
private String student_name;
private String classes_name;
private String coures;
public Student(int id, String sn, String student_name, String classes_name, String coures) {
this.id = id;
this.sn = sn;
this.student_name = student_name;
this.classes_name = classes_name;
this.coures = coures;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getClasses_name() {
return classes_name;
}
public void setClasses_name(String classes_name) {
this.classes_name = classes_name;
}
public String getCoures() {
return coures;
}
public void setCoures(String coures) {
this.coures = coures;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", sn='" + sn + '\'' +
", student_name='" + student_name + '\'' +
", classes_name='" + classes_name + '\'' +
", coures='" + coures + '\'' +
'}';
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。