美文网首页
IDEA连接mysql数据库如何配置

IDEA连接mysql数据库如何配置

作者: 司马青藤 | 来源:发表于2021-08-31 18:46 被阅读0次
image

如图所示,点击右侧边栏Database,点加号,Data Source,MySQL,弹出下图所示界面。

image

如上图所示,填写用户名,密码,数据库名,点击测试链接,连接数据库成功的话就可以了。

这时已经可以查看数据库信息了:

image

下面用代码调用sql语句去操作一下数据库:


public class JdbcDemo1 {

public static void main(String[] args) {

//注册驱动

        Connection conn =null;

        Statement statement =null;

try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            String sql ="insert into account values(3,'赵六',234999)";

            conn = DriverManager.getConnection("jdbc:mysql:///db1","root","xxxxxx");//数据库,用户名,密码

            statement = conn.createStatement();

            int count = statement.executeUpdate(sql);

            System.out.println(count);

            if (count >0) {

                    System.out.println("执行成功");

            }else {

                    System.out.println("执行失败");

            }

}catch (ClassNotFoundException|SQLException e) {

e.printStackTrace();

        }finally {

try {

if (statement !=null) statement.close();

                if (conn !=null) conn.close();

            }catch (SQLException e) {

e.printStackTrace();
            }
}
}
}

执行完,查看数据库验证,发现数据插入成功!

相关文章

网友评论

      本文标题:IDEA连接mysql数据库如何配置

      本文链接:https://www.haomeiwen.com/subject/ikoziltx.html