JDBC链接Mysql数据库的六个步骤

jdbc链接mysql数据库的步骤

  1. 加载驱动
		//指定驱动方式
		Class.forName("com.mysql.jdbc.Driver");
  1. 建立链接 url 用户名 密码
		String url="jdbc:mysql://127.0.0.1:3306/test1";
		String usename="root";
		String password="root";
		Connection con=DriverManager.getConnection(url,usename,password);
  1. 创建Statement对象 ,(或者是执行sql语句的平台)
		Statement sta=con.createStatement();
  1. 执行sql语句
	
		int row =sta.executeUpdate("insert into userinfo values('bob','789','[email protected]','22','man')");
		//ResultSet rs=sta.executeQuery("select Id from usermessage");
  1. 第五步处理执行结果
		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. 关闭链接
		sta.close();
		con.close()
  • jdbc链接oracle数据库的操作 只有在加载驱动和建立链接有区别

猜你喜欢

转载自blog.csdn.net/weixin_43941676/article/details/88910111