import java.sql.*;//导入sql包
public classconnect {public static void main(String args[]) throwsSQLException, ClassNotFoundException
{//数据库连接
Statement sql;//数据库
ResultSet rs;//数据
Connection conn;//用于连接
String url = "jdbc:mysql://localhost:3306/students?serverTimezone=UTC&useSSL=false";
String username= "root";
String password= "123456";//密码//Class.forName("com.mysql.cj.jdbc.Driver");//加载JDBC-MySQL驱动
conn = DriverManager.getConnection(url,username,password);//连接完毕//添加筛选条件
String c1 = " year(birthday)<=2000 and month(birthday)>7";
String c2= " name Like '张_%' ";
String c3= " height >1.65";
String sqlStr="select * from mess where" +c1+ " and "+c2+ " and "+c3+"order by birthday";try{
sql=conn.createStatement();
rs=sql.executeQuery(sqlStr);while(rs.next())
{
String number=rs.getString(1);
String name=rs.getString(2);
Date date=rs.getDate(3);float height=rs.getFloat(4);
System.out.printf("%s\t",number);
System.out.printf("%s\t",name);
System.out.printf("%s\t",date);
System.out.printf("%.2f\t",height);
System.out.printf("\n");
}//conn.close();
}catch(SQLException e)
{
System.out.println(e);
}
System.out.println("--------华丽的分割线---------");/*try{
Class.forName("com.mysql.cj.jdbc.Driver");//加载JDBC-MySQL驱动
conn = DriverManager.getConnection(url,username,password);
if(conn != null){
System.out.println("数据库连接成功!");
}else{
System.out.println("数据库连接失败!");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}*/
//顺序查询
try{//conn = DriverManager.getConnection(url,username,password);
sql =conn.createStatement();
rs= sql.executeQuery("SELECT*FROM mess");while(rs.next()) {
String number=rs.getString(1);
String name=rs.getString(2);
Date date=rs.getDate(3);float height=rs.getFloat(4);
System.out.printf("%s\t",number);
System.out.printf("%s\t",name);
System.out.printf("%s\t",date);
System.out.printf("%.2f\t",height);
System.out.printf("\n");
}
conn.close();
}catch(SQLException e)
{
System.out.println(e);
}
}
}