JDBC的安装与使用

JDBC的安装

   首先在登录MySQL的官网下载JDBC-MySQL数据库驱动,或者去www.mysql.com/products/connector直接下载。

   因为jdbc包属于第三方包,因此要自己导入,下面是导入的方法:

https://jingyan.baidu.com/article/3aed632e1a4ceb70108091f6.html

       导入之后就创建一个connect类来编写代码,来测试是否能与服务器连接。

  

import java.sql.*;//导入sql包
public class connect {
    
    public static void main(String args[]) throws SQLException, 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);//连接完毕
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();
        }
}
}

如果数据库可以连接之后就可以来试一下数据库的基本操作;

import java.sql.*;//导入sql包
public class connect {
    
    public static void main(String args[]) throws SQLException, 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);
        
    }
    
    
}
}

可能会遇到的问题:

    JDBC连接MYSQL数据库失败,Loading class `com.mysql.jdbc.Driver'. This is deprecated.

      https://blog.csdn.net/weixin_42323802/article/details/82589743

 


posted @ 2019-09-06 15:45  PRINT王哲  阅读(5012)  评论(1编辑  收藏  举报
Live2D