mysql jdbc安装和使用_JDBC的安装与使用

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值