Home > Article > Backend Development > How to connect mysql through idea to implement simple CRUD
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.
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!