Intellij IDEA通过java操作mysql数据库

一、在项目中导入驱动包

**1、在mysql数据库默认安装位置(我的是C:\Program Files (x86)\MySQL\Connector J 8.0)复制
mysql-connector-java-8.0.11.jar;将其放到项目目录下,
2、在IDEA:file->Project Structure->Moudles->dependencies中添加选项卡中选择jars;

二、通过JDBC操作mysql数据库

实现代码如下:

import java.sql.*;

public class java_jdbc {
    private  static final String url="jdbc:mysql://localhost:3306/db_infomanager?useSSL=false&characterEncoding=utf-8&serverTimezone=Hongkong";
    private static final String UserNmae="root";
    private static final String pwd="0000";

    public static void update(){
        Connection connection=null;
        Statement stmt=null;

        ResultSet rst=null;
        try {
            //a.import the driver package and load the specific driver Class
            Class.forName("com.mysql.cj.jdbc.Driver");   //load the specific driver class;
            //b.Establish a connection to the database;
            connection = DriverManager.getConnection(url,UserNmae,pwd);
            //c.send sql,execute;
            stmt=connection.createStatement();
           // String sql="insert into  tb_student values('2015310012',"+"'沈天华'"+","+"'男'"+",25,"+"'黑龙江'"+",'汉','AC1302')";
            //String sql="delete from tb_student where studentNo="+"'2015310012'"+";
            String sql="select * from tb_student";
            //d.execute sql;
           // int count=stmt.executeUpdate(sql);//can only be use for: insert,delete,update;
            //f.resultset ;if the way you do it is select,you should use this method:  stmt.executeQuery(sql);
             rst=stmt.executeQuery(sql);
            //e.result of handing;
            while(rst.next()){
                String name= rst.getString("studentName");
                int age=rst.getInt("age");
                System.out.println(name+"----"+age);
            }
           /* if(count>0){
                System.out.println("successful!");
            }else {
                System.out.println("defeat!");
            }*/
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();;
        }
        finally {
            try {
                if(rst!=null)
                    rst.close();
                if (stmt!=null)
                    stmt.close();
                if(connection!=null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static  void main(String[] args){
        update();
    }
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

It&code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值