Six steps of JDBC linking Mysql database

Steps to link jdbc to mysql database

  1. Load the driver
		//指定驱动方式
		Class.forName("com.mysql.jdbc.Driver");
  1. Establish link url username password
		String url="jdbc:mysql://127.0.0.1:3306/test1";
		String usename="root";
		String password="root";
		Connection con=DriverManager.getConnection(url,usename,password);
  1. Create a Statement object, (or a platform for executing SQL statements)
		Statement sta=con.createStatement();
  1. Execute sql statement
	
		int row =sta.executeUpdate("insert into userinfo values('bob','789','[email protected]','22','man')");
		//ResultSet rs=sta.executeQuery("select Id from usermessage");
  1. The fifth step is to process the execution result
		ResultSet rs=sta.executeQuery("Select*from userinfo");
		while(rs.next()) {
    
    
			String name=rs.getString("username");
			String sex=rs.getString("sex");
			//打印输出
			System.out.println("姓名:"+name+"性别:"+sex);
		}
  1. Close link
		sta.close();
		con.close()
  • The operation of jdbc linking oracle database is only different in loading the driver and establishing the link

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/88910111