Home >Backend Development >PHP Tutorial >How to connect mysql through idea to implement simple CRUD

How to connect mysql through idea to implement simple CRUD

不言
不言Original
2018-04-14 10:35:292836browse

The content of this article is about how to connect mysql through idea to implement simple CRUD. It has certain reference value. Friends in need can refer to it.

Junior Postgraduate Entrance Examination Dog, I didn’t want to write it originally Blogging is a waste of time. After thinking about it, I decided to use my spare time to write.

Many newbies, maybe new to idea, think wow, it’s so powerful, and it’s true. But it may not be so easy to use. Here, I will briefly introduce what the title says.

1. First, create a database under mysql named: studentdbtest, and create a table named: Student; as shown below:

2. Open idea and create a project named: project02_dbtest (I chose java Enterprise -> web application, which means I created a web project. Of course, you can also create other projects) as shown in the picture:


3. Next, you need to import the mysql jar package, as shown in the figure. First, create a new lib package (Directory) in the web_INF directory and import the mysql driver (the mysql you downloaded Just copy and paste the jar package under the lib package), Then, a very important step, select the mysql jar package you just imported, right-click, select Add as Library, a window will pop up, just select ok . So far, all the preparations have been done, and the next step is to code.




#4. Create the CRUD package under src and create a query query.java, as shown in the figure. I won't explain the code below, the comments are also very clear.



Code part:

public class query {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        //1.注册数据库的驱动
//        Driver  driver=new com.mysql.jdbc.Driver();
//        DriverManager.registerDriver(driver);
        Class.forName("com.mysql.jdbc.Driver");


        //2.获取数据库连接
        String  url="jdbc:mysql://localhost:3306/StudentdbTest?useSSL=false";
        Connection connection = DriverManager.getConnection(url, "root", "root123");

        //3.获取数据库的对象
        Statement statement = connection.createStatement();
        String sql="select *from student";
        ResultSet resultSet = statement.executeQuery(sql);
//        Statement statement = JDBCUtil.getConnection.createStatement();
//        String sql="select *from student";
//        ResultSet resultSet = statement.executeQuery(sql);
        //4.遍历结果集,取出数据
        while(resultSet.next()){
            String p_id = resultSet.getString("P_Id");
            String p_name = resultSet.getString("P_Name");
            System.out.println("学号:"+p_id+"姓名:"+p_name);
        }

        //5.关闭数据库连接
        resultSet.close();
        statement.close();
        connection.close();
        //JDBCUtil.getConnection.close();

    }
}

5. As for the additions, deletions and modifications, they are almost the same, here they are No more writing.

Related recommendations:

IDEA how to install vue development plug-in installation graphic tutorial


The above is the detailed content of How to connect mysql through idea to implement simple CRUD. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn