seversql database connection

Then in the previous article, we can add the following code directly after "" database connection is successful "to query the table in the database

在这里插入代码片
Statement s=dbConn.createStatement();
		s.executeUpdate("create table student(name varchar(20),sex varchar(5))");//建表
         ResultSet r = s.executeQuery("Select name from student where sex='男'");
         while(r.next()) {
        	 System.out.println(r.getString(11));
         }
         dbConn.close();

At the same time, query and update can use PrepareStatement

在这里插入代码片
PreparedStatement s=dbConn.prepareStatement("Select name from student where sex=?");//“?”做为参数的占位符
	     s.setString(1,"男");

The PreparedStatement interface is an extension of the Statement interface and is used to execute pre-compiled SQL statements with or without parameters. The executeQuery () and executeUpdate () methods are similar to the two methods defined in the Statement interface, except that they have no parameters because the SQL statement was specified in the preparedStatenent method when the PreparedStatement object was created.

Published 152 original articles · praised 16 · 30,000+ views

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/105440733